Kindly clarify my doubt as i got so confused with the below stuff and i couldnt get a clean anwser any where else on net.
#include<stdio.h>
int main()
{
int a = 0x44332211;
printf("Network - 0x%x\n", htonl(a));// Host to network
printf("Host - 0x%x\n", ntohl(a));// Network to host
return 0;
}
Output:
Network - 0x11223344
Host - 0x11223344
Here htonl(0x44332211)
=> i am converting little endian(LE) to BE. So output will be 0x11223344
. That i understood. My problem is with ntoh()
. Now ntohl(0x44332211)
=> what?
Here i am executing both the commands on 1 terminal. So host to network, ie hton()
means my terminal to network. That makes sense. But here ntohl()
means what? ntohl()
comes into picture if we have:
a PC A----(ie hton)sending data over network------>(ie ntoh) to PC B?
Also ntoh
expects a network byte order ie Big endian. Kindly intepret what ntohl()
means above and why its printed same as 0x11223344
and why not 0x44332211
?