Maanu
Maanu

Reputation: 5203

Hexa decimal string format

Is it possible to directly convert integer to hexadecimal in the following way?

Integer Value = 145635

hexa decimal value (4 bytes) = 0x000238E3

<0x00><0x02><0x38><0xE3>

I have tried with %x. But that doesn't work for me.
Can anyone help me on this issue?

Upvotes: 0

Views: 99

Answers (1)

Stephan Lechner
Stephan Lechner

Reputation: 35164

You can achieve the output with the <..> surrounding each byte at least with one printf-statement as follows. Note that data type is unsigned, which is better in conjunction with hex and with bit shift operators:

unsigned int val = 145635; 
// "ordinary" printf  
printf("%08X\n", val);       

// printf per byte:
printf("<%02X><%02X><%02X><%02X>\n",
       val >> 24,
       (val&0x00ff0000) >> 16,
       (val&0x0000ff00) >> 8,
       val&0x000000ff);

Upvotes: 2

Related Questions