/***************************************************************************/ /* Example of Formatted Hex Dump */ /* */ /* BinToHex.c */ /* */ /* Version : 1.1 */ /* */ /* By : Julian Winpenny */ /* */ /* Date : 8 Jan 2001 */ /* */ /* Purpose : Display data from buffer/string in hexadecimal format */ /* to an RS232 terminal device */ /* */ /* Revision history : */ /* */ /* Changes from version 1. */ /* renamed "rows" to "cols" in function FormatedHexDump( ) */ /* */ /* */ /* */ /***************************************************************************/ #define CR 0x0d void main(void) { char c; char buffer[] = {'T','h','i','s',' ','i','s','a',' ','t','e','s','t','.' }; for( c=0; c < 14; c++ ) // Display buffer as text { Putchar( buffer[c] ); } Putchar( CR ); // New line FormatedHexDump( buffer, 14 ); // Display buffer a Hexadecimal while(1) { // Wait here....... } } //************************************************** // FormatedHexDump( char *buf, char len ) // // // Generate a formatted hex dump of a buffer // or string. // // Input: *buf : pointer to buffer to display // len : Number of bytes to dump // // Typical output to RS232 device... // // 6A,7B,09,98,8B,76,45,A8,00,78,67,C6,FF,FF,FF,00 // 00,01,02,09,89,F6,B7,C1,FF,00,04,00,00,FF,F3,00 // // Note: Maximum buffer lengh 255 // //************************************************** void FormatedHexDump( char *buf, char len ) { char p; // Index in buffer char Cols; // Column counter Cols = 0; // Reset Column counter p = 0; // Index first byte to dump // Repeat for number of bytes required while( p < len ) { BinToHex( buf[p] ); // Display as HEX p++; // Next byte to decode if ( Cols++ == 16 ) // New line after each 16 columns { PutChar( CR ); Cols = 0; } else if ( p != len ) // If were not at the end of the buf { // and there is no new line PurChar(','); // output a ',' } } PutChar( CR ); // Final new line } //******************************************************************* // BinToHex( char ) // // Convert a 8 bit binary number to Hexadecimal ASCII representation // ( 2 ASCII characters ) // // Input: binary - byte to convert // e.g. 0x2A binary converts to 0x32 + 0x41 // // Outputs to the RS232 port using SendChar(char) // // Note: // Outputs the result to the RS232 port. // //******************************************************************* void BinToHex( char b ) { const char HexDigits[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; PutChar( HexDigits[ b >> 4 & 0x0f ] ); // Convert upper nibble to hex digit. // Right shift 4 bits and mask off upper nibble, // then index value in HexDigits array. PutChar( HexDigits[ b & 0x0f ] ); // Convert lower nibble to Hex digit // Mask off upper nibble, // then index value in HexDigits array. }