I ran the following program on little-endian [LE] machine [Linux, Intel processor]. I am unable to explain the 3 outputs in below code snippet. Since machine is LE, the value of a
is stored as 0x78563412
. When printing, it is displaying its actual value. Since its an LE machine, I expect ntohl()
to be a no-op and display 0x78563412
, which it is doing. However, I expect 0x12345678
for 2nd print statement containing htonl()
. Can someone please help me understand why they are same?
int main()
{
int a = 0x12345678;
printf("Original - 0x%x\n", (a));
printf("Network - 0x%x\n", htonl(a));
printf("Host - 0x%x\n", ntohl(a));
return 0;
}
Output:
Original - 0x12345678
Network - 0x78563412
Host - 0x78563412
x == htonl(ntohl(x))
. But this is not happening and your explanation was very helpful. – Deli