08-27-2008
1,
0
Join Date: Aug 2008
Last Activity: 27 August 2008, 9:45 AM EDT
Posts: 1
Thanks Given: 0
Thanked 0 Times in 0 Posts
htons and ntohs
Hi All,
I am running a short program on the effects of host-to-network and network-to-host functions. The following is part of my code:
int a = 384;
printf("Used htons with a, getting: %x Hex and %d Numeral\n", htons(a), htons(a));
printf("Used ntohs with a, getting: %x Hex and %d Numeral\n", ntohs(a), ntohs(a));
printf("Used htons(ntohs()) with a, getting: %x Hex and %d Numeral\n", htons(ntohs(a)), htons(ntohs(a)));
printf("Used ntohs(htons()) with a, getting: %x Hex and %d Numeral\n", ntohs(htons(a)), ntohs(htons(a)));
The following are the results:
Used htons with a, getting: 8001 Hex and 32769 Numeral
Used ntohs with a, getting: 8001 Hex and 32769 Numeral
Used htons(ntohs()) with a, getting: 180 Hex and 384 Numeral
Used ntohs(htons()) with a, getting: 180 Hex and 384 Numeral
I am using the program on a little-endian machine. I understand how 8001H and 32769 are derived but why are the first 2 results the same??