/* C program to swap bytes/words of integer number.
How many bytes is 0xff?
0xff is a number represented in the hexadecimal numeral system (base 16).
It's composed of two F numbers in hex. As we know,
F in hex is equivalent to 1111 in the binary numeral system.
So, 0xff in binary is 11111111.
Lets break the format code "%04X" into its separate parts:
The X means that it will print an integer, in hexadecimal, large X for large hexadecimal letters
The 4 means the number will be printed left justified with at least four digits, print spaces if there is less than four digits
The 0 means that if there is less than four digits it will print leading zeroes.
*/
#include <stdio.h>
int main() {
int val = 0xABCD;
int ext1 = (val & 0xFF00) >> 8;
int ext2 = (val & 0x00FF);
int res = (ext2<<8) + (ext1);
printf("%X",res);
}
#if 0
int main()
{
unsigned int data = 0x1234;
printf("\ndata before swapping : %04X", data);
//0xff00 = HHHH HHHH 0000 0000
data = ((data << 8) & 0xff00) | ((data >> 8) & 0x00ff);
//0010 0011 0100 0000 -> 0010 0011 0000 0000
printf("\ndata after swapping : %04X", data);
return 0;
}
int d = 0x1234;
d = ((d<<8) & 0xff00) | ((d>>8) & 0x00ff);
printf("%x",d);
#endif
To embed this program on your website, copy the following code and paste it into your website's HTML: