Created
December 29, 2025 12:26
-
-
Save LightningStalker/0e6a96e5b423cb2187975095c044c224 to your computer and use it in GitHub Desktop.
C++ hex dump for debug, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Compile with $ g++ -o hxd hxd.cpp | |
| * Demonstration of the hexadecimal dumper for the char[] | |
| * Project Crew™ 12/28/2025 | |
| */ | |
| #include <iostream> | |
| #include <iomanip> | |
| #include <string> | |
| using namespace std; | |
| #include "hxd.hpp" | |
| #define MYB "\x5a\xed\x44\x7d\x3b\x39\xe5\x8b" \ | |
| "\x04\xdb\xf7\x40\x61\x18\x42\x90" \ | |
| "Lorem ipsum dolor si\x88""t amet, " \ | |
| "conse\x8""ctet\x10""uer adi\x13""piscing el\xed""it." | |
| int | |
| main() | |
| { | |
| unsigned char myb[] = MYB; | |
| /* pass in the array of character */ | |
| hxd(myb, sizeof(myb)); | |
| /* try print the array as raw */ | |
| cout << "\n\n"; | |
| cout << " string originale:\n\n"; | |
| cout << " " << myb << endl; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* hex dump | |
| * Project Crew™ 12/28/2025 | |
| */ | |
| void | |
| hxd(unsigned char * myb, unsigned long int bytec) | |
| { /* dumps contents of a char array */ | |
| unsigned long int i, j; | |
| bytec--; | |
| cout << hex << setfill('0'); | |
| i = 0; | |
| while(i < bytec) | |
| { /* begin w/byte offset */ | |
| cout << setw(8) << i << ":"; | |
| for(j = 0; j < 4; j++) | |
| { /* first half of hexi */ | |
| cout << ' ' << setw(2) << static_cast<uint16_t>(myb[i]); | |
| if (++i >= bytec) break; // break if done | |
| cout << setw(2) << static_cast<uint16_t>(myb[i]); | |
| if (++i >= bytec) break; | |
| } /* middle ... */ | |
| if (i >= bytec) break; | |
| cout << '.'; | |
| for(j = 0; j < 4; j++) | |
| { /* second half ... */ | |
| cout << setw(2) << static_cast<uint16_t>(myb[i]); | |
| if (++i >= bytec) break; | |
| cout << setw(2) << static_cast<uint16_t>(myb[i]) << ' '; | |
| if (++i >= bytec) break; | |
| } | |
| if (i >= bytec) break; | |
| cout << ' '; | |
| for (j = i - 16; j < i; j++) // print ASCII at end | |
| { | |
| if (myb[j] >= ' ' && myb[j] < '~') cout << myb[j] | |
| ;else cout << '.'; // unprintable chars = '.' | |
| } | |
| cout << endl; | |
| } | |
| /* last line is bit special */ | |
| i = bytec % 16; | |
| if(i) /* we have a partial line? */ | |
| { | |
| /* ugly but we need to find how many to the ASCII area */ | |
| for(j = 41 - (i * 2 + (i < 9 ? (i - 1) / 2 : i / 2)); j > 0; j--) | |
| { /* equation only needs to be done one time anyway */ | |
| cout << ' '; | |
| } | |
| }else | |
| { /* hex area was full */ | |
| i = 16; | |
| cout << ' '; | |
| } /* only need 1 more space */ | |
| for(j = bytec - i; j < bytec; j++) | |
| { /* print last of the chars */ | |
| if (myb[j] >= ' ' && myb[j] < '~') cout << myb[j] | |
| ;else cout << '.'; // unprintable = '.' | |
| } | |
| } | |
| /* end hexdump */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment