0

how to convert in c++ int value to 4 byte representation.

I tried in that way:

int digit = 2;
unsigned char a[4];
a[3] = (digit>>24) & 0xFF;
a[2] = (digit>>16) & 0xFF;
a[1] = (digit>>8) & 0xFF;
a[0] = digit & 0xFF;

cout << a[0] << a[1] << a[2] << a[3] << endl;

but cout don't display anything.

Robert
  • 338
  • 5
  • 16
  • try to case your elements to int while displaying so c wont show you 1 not as 1 but as graphical representation of ascii 1 code – Toumash Feb 19 '16 at 21:43
  • ok, but if I do this like that: cout << (int)a[0] << (int)a[1] << (int)a[2] << (int)a[3] << endl; then the result is: 2000 so leading zeros are not displayed. It should be 02000000 (I think so) – Robert Feb 19 '16 at 21:46
  • http://stackoverflow.com/questions/530614/print-leading-zeros-with-c-output-operator-printf-equivalent – Toumash Feb 19 '16 at 21:52

0 Answers0