6

Display a file in hexadecimal

  1. #include "dump.h"
  2.  
  3. #include <ctype.h>
  4. #include <unistd.h>
  5.  
  6. void dump( unsigned char *buf, int size, FILE *fout ) {
  7.     register int n, i, col = 0;
  8.  
  9.     const int ncols = 16;
  10.     unsigned char c[ 16 ];
  11.  
  12.     for( n = 0; n < size; n++ ) {
  13.         fprintf( fout, "%02x ", buf[ n ] );
  14.         c[ col ] = buf[ n ];
  15.         col++;
  16.         if( col == ncols / 2 )
  17.             fprintf( fout, " " );
  18.         else if( col == ncols ) {
  19.             for( i = 0; i < col; i++ )
  20.                 fprintf( fout, "%c", isprint(c[ i ] ) ? c[ i ] : '.' );
  21.             fprintf( fout, "\n" );
  22.             col = 0;
  23.         }
  24.     }
  25.     if( col != 0 ) {
  26.         for( i = ncols - col; i > 0; i-- )
  27.             fprintf( fout, "   " );
  28.         if( col < ncols / 2 )
  29.             fprintf( fout, " " );
  30.         for( i = 0; i < col; i++ )
  31.             fprintf( fout, "%c", isprint( c[ i ] ) ? c[ i ] : '.' );
  32.         fprintf( fout, "\n" );
  33.     }
  34. }
  35.  
  36. #if defined( STANDALONE )
  37.  
  38. #if 0
  39. #include <io.h>
  40. #endif
  41.  
  42. #include <fcntl.h>
  43. #include <errno.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46.  
  47. int main( int argc, char *argv[] ) {
  48.     unsigned char buf[ 4096 ];
  49.     int n;
  50.  
  51.     int fd = 0; /* default to stdin */
  52.  
  53.     switch( argc ) {
  54.         case 1:
  55.             break;
  56.         case 2:
  57.             fd = open( argv[ 1 ], O_RDONLY );
  58.             if ( fd == -1) {
  59.                 fprintf( stderr, "%s\n", strerror( errno ) );
  60.                 exit( 2 );
  61.             }
  62.             break;
  63.         default:
  64.             fprintf( stderr, "%s [file]\n", argv[0] );
  65.             exit(1);
  66.     }
  67.  
  68.     while ( (n = read( fd, buf, sizeof( buf ))) != 0 )
  69.         dump( buf, n, stdout );
  70.     exit( 0 );
  71. }
  72.  
  73. #endif
$ gcc -DSTANDALONE -o dump dump.c
$ ./dump < dump.c
23 69 6e 63 6c 75 64 65  20 22 64 75 6d 70 2e 68 #include "dump.h
22 0a 0a 23 69 6e 63 6c  75 64 65 20 3c 63 74 79 "..#include <cty
70 65 2e 68 3e 0a 23 69  6e 63 6c 75 64 65 20 3c pe.h>.#include <
75 6e 69 73 74 64 2e 68  3e 0a 0a 76 6f 69 64 20 unistd.h>..void 
64 75 6d 70 28 20 75 6e  73 69 67 6e 65 64 20 63 dump( unsigned c
68 61 72 20 2a 62 75 66  2c 20 69 6e 74 20 73 69 har *buf, int si
7a 65 2c 20 46 49 4c 45  20 2a 66 6f 75 74 20 29 ze, FILE *fout )
...

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].