Skip to content

Instantly share code, notes, and snippets.

@janpipek
Created June 13, 2012 07:57
Show Gist options
  • Save janpipek/2922650 to your computer and use it in GitHub Desktop.
Save janpipek/2922650 to your computer and use it in GitHub Desktop.
How to print byte representation of a simple struct in C++
#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