c++ - How convert unsigned int to unsigned char array -
i need extract bytes using bitwise & operator. 0xff hexadecimal mask extract 1 byte. 2 bytes, code working correctly:
#include <stdio.h> int main() { unsigned int = 0x7ee; unsigned char c[2]; c[0] = & 0xff; c[1] = (i>>8) & 0xff; printf("c[0] = %x \n", c[0]); printf("c[1] = %x \n", c[1]); return 0; }
output:
c[0] = ee; c[1] = 7;
what should 4 bytes work correctly?
unsigned int = 0x557e89f3; unsigned char c[4];
my code:
unsigned char c[4]; c[0] = & 0xff; c[1] = (i>>8) & 0xff; c[2] = (i>>16) & 0xff; c[3] = (i>>24) & 0xff; printf("c[0] = %x \n", c[0]); printf("c[1] = %x \n", c[1]); printf("c[2] = %x \n", c[2]); printf("c[3] = %x \n", c[3]);
#include <stdio.h> int main() { unsigned int = 0x557e89f3; unsigned char c[4]; c[0] = & 0xff; c[1] = (i>>8) & 0xff; c[2] = (i>>16) & 0xff; c[3] = (i>>24) & 0xff; printf("c[0] = %x \n", c[0]); printf("c[1] = %x \n", c[1]); printf("c[2] = %x \n", c[2]); printf("c[3] = %x \n", c[3]); return 0; }
Comments
Post a Comment