Skip to main content
formato de código
Origen Enlace
Eduen Sarceño
  • 2.1k
  • 12
  • 21
#include <ctype.h> // isprint
#include <stdio.h>  // printf
#include <stdlib.h> // size_t

#define LINE_IN_BYTES         0x10 
#define WORD_IN_BYTES         2
#define HEX_DIGIT_PER_CHAR    2

#define IS_ODD(x) (x % 2 == 1)
       
              void print_memory (const void*, size_t);
static        void print_ascii (const void*, size_t);
static inline void print_spaces (size_t);

int
main (void)
{
  int tab[10] = {0, 23, 150, 255, 12, 16, 21, 42};
  char m[17] = "Hola mundo";

  print_memory (tab, sizeof(tab));
  print_memory (m, sizeof(m));  

  return 0;
}

void
print_memory (const void *addr, size_t size)
{
  const unsigned charsize_t *c;i;
  size_t i;resto;
  size_tconst resto;unsigned char *c;

  if (addr == NULL)
      return; 

  for (i = 1; i <= size; i++)
    {
      c = (addr + i - 1);
      printf ("%02hhx", *c);

      if (i % WORD_IN_BYTES == 0)
        {
          putchar (' ');
        }

      if (i % LINE_IN_BYTES == 0)
        {
          print_ascii (c - LINE_IN_BYTES + 1, LINE_IN_BYTES);
          printf ("\n");
        }
    }

    if( ((resto = size % LINE_IN_BYTES) > 0) // falta ascii que imprimir
      {
        print_spaces ((HEX_DIGIT_PER_CHAR + 0.5) * (LINE_IN_BYTES - resto));
     // rellena lo faltante
        if(IS_ODD (resto))
            putchar (' '); // alinea a palabra 
        
        print_ascii (c - resto + 1, resto);
        printf ("\n");
      }
}


static void 
print_ascii (const void *addr, size_t size)
{
  size_t i;
  const unsigned char *c;
  size_t i;

  for (i = 0; i < size; i++)
    {
      c = (addr + i);
      if (isprint (*c))
        putchar (*c);
      else
        putchar ('.');
 
    } 
}

static inline 
void 
print_spaces (size_t size)
{
  intsize_t i;
  for (i = 0; i < size; i++)
    {
      putchar(' ');   
    }
}
#include <ctype.h> // isprint
#include <stdio.h>  // printf
#include <stdlib.h> // size_t

#define LINE_IN_BYTES         0x10 
#define WORD_IN_BYTES         2
#define HEX_DIGIT_PER_CHAR    2

#define IS_ODD(x) (x % 2 == 1)
       
              void print_memory(const void*, size_t);
static        void print_ascii(const void*, size_t);
static inline void print_spaces(size_t);

int
main(void)
{
  int tab[10] = {0, 23, 150, 255, 12, 16, 21, 42};
  char m[17] = "Hola mundo";

  print_memory (tab, sizeof(tab));
  print_memory (m, sizeof(m));  

  return 0;
}

void
print_memory(const void *addr, size_t size)
{
  const unsigned char *c;
  size_t i;
  size_t resto;

  if (addr == NULL)
      return; 

  for (i = 1; i <= size; i++)
    {
      c = (addr + i - 1);
      printf ("%02hhx", *c);

      if (i % WORD_IN_BYTES == 0)
        {
          putchar (' ');
        }

      if(i % LINE_IN_BYTES == 0)
        {
          print_ascii (c - LINE_IN_BYTES + 1, LINE_IN_BYTES);
          printf ("\n");
        }
    }

    if( (resto = size % LINE_IN_BYTES) > 0) // falta ascii que imprimir
      {
        print_spaces ((HEX_DIGIT_PER_CHAR + 0.5) * (LINE_IN_BYTES - resto));
        
        if(IS_ODD(resto))
            putchar(' '); // alinea a palabra 
        
        print_ascii (c - resto + 1, resto);
        printf("\n");
      }
}


static void 
print_ascii(const void *addr, size_t size)
{
  const unsigned char *c;
  size_t i;

  for (i = 0; i < size; i++)
    {
      c = (addr + i);
      if (isprint (*c))
        putchar (*c);
      else
        putchar ('.');
 
    } 
}

static inline 
void 
print_spaces(size_t size)
{
  int i;
  for (i = 0; i < size; i++)
    {
      putchar(' ');   
    }
}
#include <ctype.h> // isprint
#include <stdio.h>  // printf
#include <stdlib.h> // size_t

#define LINE_IN_BYTES         0x10 
#define WORD_IN_BYTES         2
#define HEX_DIGIT_PER_CHAR    2

#define IS_ODD(x) (x % 2 == 1)
       
              void print_memory (const void*, size_t);
static        void print_ascii (const void*, size_t);
static inline void print_spaces (size_t);

int
main (void)
{
  int tab[10] = {0, 23, 150, 255, 12, 16, 21, 42};
  char m[17] = "Hola mundo";

  print_memory (tab, sizeof(tab));
  print_memory (m, sizeof(m));  

  return 0;
}

void
print_memory (const void *addr, size_t size)
{
  size_t i;
  size_t resto;
  const unsigned char *c;

  if (addr == NULL)
      return; 

  for (i = 1; i <= size; i++)
    {
      c = (addr + i - 1);
      printf ("%02hhx", *c);

      if (i % WORD_IN_BYTES == 0)
        {
          putchar (' ');
        }

      if (i % LINE_IN_BYTES == 0)
        {
          print_ascii (c - LINE_IN_BYTES + 1, LINE_IN_BYTES);
          printf ("\n");
        }
    }

    if ((resto = size % LINE_IN_BYTES) > 0) // falta ascii que imprimir
      {
        print_spaces ((HEX_DIGIT_PER_CHAR + 0.5) * (LINE_IN_BYTES - resto)); // rellena lo faltante
        if(IS_ODD (resto))
            putchar (' '); // alinea a palabra 
        print_ascii (c - resto + 1, resto);
        printf ("\n");
      }
}


static void 
print_ascii (const void *addr, size_t size)
{
  size_t i;
  const unsigned char *c;

  for (i = 0; i < size; i++)
    {
      c = (addr + i);
      if (isprint (*c))
        putchar (*c);
      else
        putchar ('.');
    } 
}

static inline 
void 
print_spaces (size_t size)
{
  size_t i;
  for (i = 0; i < size; i++)
    {
      putchar(' ');   
    }
}
formato de la salida.
Origen Enlace
Eduen Sarceño
  • 2.1k
  • 12
  • 21
void
print_memory (const void *addr, size_t size)
{
  for(cada byte en [addr, addr + size])
   {
      imprimir byte en hex
      
      if(se hanha imprimido 16B2B)
        {
          imprimir losespacio
 16B anteriores en ascii
    }
      imprimir 
 salto de linea
   if(se han imprimido 16B)
  }
      else{
 if(se ha imprimido 2B)
      imprimir los 16B {anteriores en ascii
          imprimir espaciosalto de linea
        } 

   }
   imprimir en forma alineada los ascii de la línea incompleta
   imprimir salto de linea
}
#include <ctype.h> // isprint
#include <stdio.h>  // printf
#include <stdlib.h> // size_t

#define LINE_IN_BYTES         0x10 
#define WORD_IN_BYTES         2
#define HEX_DIGIT_PER_CHAR    2

#define IS_ODD(x) (x % 2 == 1)
       
              void print_memory(const void*, size_t);
static        void print_ascii(const void*, size_t);
static inline void print_spaces(size_t);

int
main(void)
{
  int tab[10] = {0, 23, 150, 255, 12, 16, 21, 42};
  char m[17] = "Hola mundo";

  print_memory (tab, sizeof(tab));
  print_memory (m, sizeof(m));   

  return 0;
}

void
print_memory(const void *addr, size_t size)
{
  const unsigned char *c;
  size_t i;
  size_t resto;

  if (addr == NULL || size == 0)
    {
      return; 
    }

  for (i = 1; i <= size; i++)
    {
      c = (addr + i - 1);
      printf ("%02hhx", *c);

      if (i % LINE_IN_BYTESWORD_IN_BYTES == 0)
        {
          putchar (' ');
        }

      if(i % LINE_IN_BYTES == 0)
        {
          print_ascii (c - LINE_IN_BYTES + 1, LINE_IN_BYTES);
          printf ("\n");
        }
    }

   else if( (iresto = size % WORD_IN_BYTESLINE_IN_BYTES) ==> 0)
  // falta ascii que imprimir
      {
        print_spaces ((HEX_DIGIT_PER_CHAR putchar+ 0.5) * ('LINE_IN_BYTES '- resto));
        }
    }

  print_spaces  if(IS_ODD(HEX_DIGIT_PER_CHAR + 0.5resto))
            putchar(' '); // alinea *(LINE_IN_BYTESa -palabra size 
 % LINE_IN_BYTES));      
        print_ascii (c - sizeresto %+ LINE_IN_BYTES1, size % LINE_IN_BYTESresto);
  printf      printf("\n");
      }
}


static void 
print_ascii(const void *addr, size_t size)
{
  const unsigned char *c;
  size_t i;

  for (i = 0; i < size; i++)
    {
      c = (addr + i);
      if (isprint (*c))
        putchar (*c);
      else
        putchar ('.'); 

    } 
}

static inline 
void 
print_spaces(size_t size)
{
  int i;
  for (i = 0; i < size; i++)
    {
      putchar(' ');   
    }
}
$ gcc print_memory.c -o print_memory -Wall -Werror; ./print_memory | cat -e
0000 0000 1700 0000 9600 0000 ff00 0000 ................$
0c00 0000 1000 0000 1500 0000 2a00 0000 .............*...$
0000 0000 0000 0000                     ........$
486f 6c61 206d 756e 646f 0000 0000 0000 Hola mundo......$
00                                      .$
void
print_memory (const void *addr, size_t size)
{
  for(cada byte en [addr, addr + size])
   {
      imprimir byte en hex
      
      if(se han imprimido 16B)
        {
          imprimir los 16B anteriores en ascii
          imprimir salto de linea
        }
      else if(se ha imprimido 2B)
        {
          imprimir espacio
        }
   }
   imprimir en forma alineada los ascii de la línea incompleta
   imprimir salto de linea
}
#include <ctype.h> // isprint
#include <stdio.h>  // printf
#include <stdlib.h> // size_t

#define LINE_IN_BYTES         0x10 
#define WORD_IN_BYTES         2
#define HEX_DIGIT_PER_CHAR    2

       
              void print_memory(const void*, size_t);
static        void print_ascii(const void*, size_t);
static inline void print_spaces(size_t);

int
main(void)
{
  int tab[10] = {0, 23, 150, 255, 12, 16, 21, 42};
  print_memory (tab, sizeof(tab));  
  return 0;
}

void
print_memory(const void *addr, size_t size)
{
  const unsigned char *c;
  size_t i;

  if (addr == NULL || size == 0)
    {
      return; 
    }

  for (i = 1; i <= size; i++)
    {
      c = (addr + i - 1);
      printf ("%02hhx", *c);

      if(i % LINE_IN_BYTES == 0)
        {
          putchar (' ');
          print_ascii (c - LINE_IN_BYTES, LINE_IN_BYTES);
          printf ("\n");
        }
      else if (i % WORD_IN_BYTES == 0)
         {
          putchar (' ');
        }
    }

  print_spaces ((HEX_DIGIT_PER_CHAR + 0.5)
                *(LINE_IN_BYTES - size % LINE_IN_BYTES));
  print_ascii (c - size % LINE_IN_BYTES, size % LINE_IN_BYTES);
  printf ("\n");
}


static void 
print_ascii(const void *addr, size_t size)
{
  const unsigned char *c;
  size_t i;

  for (i = 0; i < size; i++)
    {
      c = (addr + i);
      if (isprint (*c))
        putchar (*c);
      else
        putchar ('.');
    } 
}

static inline 
void 
print_spaces(size_t size)
{
  int i;
  for (i = 0; i < size; i++)
    {
      putchar(' ');   
    }
}
$ gcc print_memory.c -o print_memory -Wall -Werror; ./print_memory | cat -e
0000 0000 1700 0000 9600 0000 ff00 0000 ................$
0c00 0000 1000 0000 1500 0000 2a00 0000 .............*..$
0000 0000 0000 0000                     ........$
void
print_memory (const void *addr, size_t size)
{
  for(cada byte en [addr, addr + size])
   {
      imprimir byte en hex
      if(se ha imprimido 2B)
        {
          imprimir espacio
        }
       
      if(se han imprimido 16B)
        {
          imprimir los 16B anteriores en ascii
          imprimir salto de linea
        } 

   }
   imprimir en forma alineada los ascii de la línea incompleta
   imprimir salto de linea
}
#include <ctype.h> // isprint
#include <stdio.h>  // printf
#include <stdlib.h> // size_t

#define LINE_IN_BYTES         0x10 
#define WORD_IN_BYTES         2
#define HEX_DIGIT_PER_CHAR    2

#define IS_ODD(x) (x % 2 == 1)
       
              void print_memory(const void*, size_t);
static        void print_ascii(const void*, size_t);
static inline void print_spaces(size_t);

int
main(void)
{
  int tab[10] = {0, 23, 150, 255, 12, 16, 21, 42};
  char m[17] = "Hola mundo";

  print_memory (tab, sizeof(tab));
  print_memory (m, sizeof(m));   

  return 0;
}

void
print_memory(const void *addr, size_t size)
{
  const unsigned char *c;
  size_t i;
  size_t resto;

  if (addr == NULL)
      return; 

  for (i = 1; i <= size; i++)
    {
      c = (addr + i - 1);
      printf ("%02hhx", *c);

      if (i % WORD_IN_BYTES == 0)
        {
          putchar (' ');
        }

      if(i % LINE_IN_BYTES == 0)
        {
          print_ascii (c - LINE_IN_BYTES + 1, LINE_IN_BYTES);
          printf ("\n");
        }
    }

    if( (resto = size % LINE_IN_BYTES) > 0) // falta ascii que imprimir
      {
        print_spaces ((HEX_DIGIT_PER_CHAR + 0.5) * (LINE_IN_BYTES - resto));
        
        if(IS_ODD(resto))
            putchar(' '); // alinea a palabra  
        
        print_ascii (c - resto + 1, resto);
        printf("\n");
      }
}


static void 
print_ascii(const void *addr, size_t size)
{
  const unsigned char *c;
  size_t i;

  for (i = 0; i < size; i++)
    {
      c = (addr + i);
      if (isprint (*c))
        putchar (*c);
      else
        putchar ('.'); 

    } 
}

static inline 
void 
print_spaces(size_t size)
{
  int i;
  for (i = 0; i < size; i++)
    {
      putchar(' ');   
    }
}
$ gcc print_memory.c -o print_memory -Wall -Werror; ./print_memory | cat -e
0000 0000 1700 0000 9600 0000 ff00 0000 ................$
0c00 0000 1000 0000 1500 0000 2a00 0000 ............*...$
0000 0000 0000 0000                     ........$
486f 6c61 206d 756e 646f 0000 0000 0000 Hola mundo......$
00                                      .$
edianness
Origen Enlace
Eduen Sarceño
  • 2.1k
  • 12
  • 21
DIRECCIÓN               MAYOR
 
  4    
    0         4
  ========  ===========
              0      tab + 36
    -----------
              0      tab + 32
    -----------
             42      tab + 28
    -----------
             21      tab + 24
    -----------
             16      tab + 20
    -----------
             12      tab + 16
    -----------
            255      tab + 12   
    -----------
            150      tab +  8
    -----------
             23      tab +  4
    -----------
              0      tab +  0
  ========  ===========      
DIRECCIÓN   
MENOR

#Little endian La anterior figura para little endian sería la siguiente

               MAYOR

    0         4 
    ===========
    0x0000 0000      tab + 36
    -----------
    0x0000 0000      tab + 32
    -----------
    0x2A00 0000      tab + 28
    -----------
    0x1500 0000      tab + 24
    -----------
    0x1000 0000      tab + 20
    -----------
    0x0C00 0000      tab + 16
    -----------
    0xFF00 0000      tab + 12   
    -----------
    0x9600 0000      tab +  8
    -----------
    0x1700 0000      tab +  4
    -----------
    0x0000 0000      tab +  0
    ===========        
MENOR

#Big endian

               MAYOR

    0         4 
    ===========
    0x0000 0000      tab + 36
    -----------
    0x0000 0000      tab + 32
    -----------
    0x0000 002A     tab + 28
    -----------
    0x0000 0015      tab + 24
    -----------
    0x0000 0010      tab + 20
    -----------
    0x0000 000C      tab + 16
    -----------
    0x0000 00FF      tab + 12   
    -----------
    0x0000 0096      tab +  8
    -----------
    0x0000 0017      tab +  4
    -----------
    0x0000 0000      tab +  0
    ===========        
MENOR
DIRECCIÓN MAYOR
 
  4      0
  ========
        0      tab + 36
  --------
        0      tab + 32
  --------
       42      tab + 28
  --------
       21      tab + 24
  --------
       16      tab + 20
  --------
       12      tab + 16
  --------
      255      tab + 12   
  --------
      150      tab +  8
  --------
       23      tab +  4
  --------
        0      tab +  0
  ========        
DIRECCIÓN MENOR
               MAYOR
      
    0         4
    ===========
              0      tab + 36
    -----------
              0      tab + 32
    -----------
             42      tab + 28
    -----------
             21      tab + 24
    -----------
             16      tab + 20
    -----------
             12      tab + 16
    -----------
            255      tab + 12   
    -----------
            150      tab +  8
    -----------
             23      tab +  4
    -----------
              0      tab +  0
    ===========         
MENOR

#Little endian La anterior figura para little endian sería la siguiente

               MAYOR

    0         4 
    ===========
    0x0000 0000      tab + 36
    -----------
    0x0000 0000      tab + 32
    -----------
    0x2A00 0000      tab + 28
    -----------
    0x1500 0000      tab + 24
    -----------
    0x1000 0000      tab + 20
    -----------
    0x0C00 0000      tab + 16
    -----------
    0xFF00 0000      tab + 12   
    -----------
    0x9600 0000      tab +  8
    -----------
    0x1700 0000      tab +  4
    -----------
    0x0000 0000      tab +  0
    ===========        
MENOR

#Big endian

               MAYOR

    0         4 
    ===========
    0x0000 0000      tab + 36
    -----------
    0x0000 0000      tab + 32
    -----------
    0x0000 002A     tab + 28
    -----------
    0x0000 0015      tab + 24
    -----------
    0x0000 0010      tab + 20
    -----------
    0x0000 000C      tab + 16
    -----------
    0x0000 00FF      tab + 12   
    -----------
    0x0000 0096      tab +  8
    -----------
    0x0000 0017      tab +  4
    -----------
    0x0000 0000      tab +  0
    ===========        
MENOR
prototipos
Origen Enlace
Eduen Sarceño
  • 2.1k
  • 12
  • 21
Loading
se añadieron 870 caracteres en el cuerpo
Origen Enlace
Eduen Sarceño
  • 2.1k
  • 12
  • 21
Loading
Origen Enlace
Eduen Sarceño
  • 2.1k
  • 12
  • 21
Loading