Created
June 13, 2012 07:57
-
-
Save janpipek/2922650 to your computer and use it in GitHub Desktop.
How to print byte representation of a simple struct in C++
This file contains 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
#include <iostream> | |
struct a | |
{ | |
int b; | |
double c; | |
}; | |
using namespace std; | |
int main() | |
{ | |
a aa; | |
aa.b = 0; | |
aa.c = 0.112; | |
cout << aa.b << " | " << aa.c << endl; | |
unsigned char* pointer = (unsigned char*)&aa; | |
while (pointer < (unsigned char*)&aa + sizeof(a)) | |
{ | |
if ((pointer >= (unsigned char*)&aa + sizeof(aa.b)) && (pointer < (unsigned char*)&aa.c)) | |
{ | |
cout << " # "; | |
} | |
else | |
{ | |
cout << " " << (int)(*pointer) << " "; | |
} | |
pointer++; | |
if (pointer == (unsigned char*)&aa.c) cout << "|"; | |
} | |
cout << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment