Something closed, but I can not google. There is a hexadecimal number, for example, 0x046A. How to convert it to an array 0x00, 0x04, 0x06, 0x0A? C ++ language.
I beg your pardon. Along the way, I was wrong. Need a string like "046A"
Something closed, but I can not google. There is a hexadecimal number, for example, 0x046A. How to convert it to an array 0x00, 0x04, 0x06, 0x0A? C ++ language.
I beg your pardon. Along the way, I was wrong. Need a string like "046A"
printf ("% 04X", 0x046A); gives the output just "046A".
printf("046A"); - αλεχολυτThe honest option:
unsigned short int x = 0x046A; unsigned char b[4]; b[0] = x&0x0F; b[1] = (x >> 4)&0x0F; b[2] = (x >> 8)&0x0F; b[3] = (x >> 12)&0x0F; To a certain extent, the hack:
union { unsigned short int x; struct { unsigned char b0: 4; unsigned char b1: 4; unsigned char b2: 4; unsigned char b3: 4; }; } data; data.x = 0x046A; printf("%02X %02X %02X %02X\n",data.b0,data.b1,data.b2,data.b3); Update Once you need a string, then on pure :) C ++, you can do this:
ostringstream s; s << hex << setw(4) << setfill('0') << x; cout << s.str() << endl; or simply
char buffer[5]; sprintf(buffer,"%04hX",x); sprintf(buffer,"%04X",x) :) More difficult, but pure C ++ - ostringstream s; s << hex << setw(4) << setfill('0') << x; cout << s.str() << endl; ostringstream s; s << hex << setw(4) << setfill('0') << x; cout << s.str() << endl; - Harrymemcpy ?) - VladDSource: https://ru.stackoverflow.com/questions/511619/
All Articles
X & 0x0F, thenX=X>>4and so on until X is 0. Most likely, we should write the array backwards - Mike