I don't think that the marked questions and answers do meet my expectations The link --> Same output for htonl() and ntohl() on an integer --> says Network byte order is big-endian, host byte order is little-endian. My confusion still goes on. Also, what is host? Still not answer most of my questions. I think those voted the question for closing have misunderstood my question I've voted to reopen. Please reread and reopen the question.
I've read that network-byte-order always is represented in Big Endian. However, I didn't found formal source for that. I did a snippet to see the difference between both functions but on the same machine I get same results.
#include <stdio.h>
int main() {
//first part
printf("%d\n", ntohs(12345));
printf("%d\n", ntohs(12345) == 12345);
// second part
printf("%d\n", htons(12345));
printf("%d\n", htons(12345) == 12345);
//third part
printf("%d\n", htons(htons(12345)));
printf("%d\n", ntohs(ntohs(12345)));
//fourth part
printf("%d\n", ntohs(htons(12345)));
return 0;
}
- If network-byte-order always is represented in Big Endian, why cannot
I read printed value as
12345
in second part? tl;dr My machine is little-endian.
After I tried same code also on PowerPC which works with Big Endian, both first part and second part give same result 12345
. It is not interesting because of the machine's representation.
- What about third part? Is it De Morgan(pun intended!)? It yields same result with fourth part. So, why do we need if both functions do same task?
- What does it mean "host" in this context? My machine?
- I though that I could use the functions to check whether a machine uses little-endian or big-endian. However, It doesn't sound sanely after the intricacies.
Little-Endian Computer Output: (Mac OSX)
14640
0
14640
0
12345
12345
12345
Big-Endian Computer Output: (PowerPC)
root@debian-powerpc:~# gcc -c endian.c
root@debian-powerpc:~# gcc endian.c -o out
root@debian-powerpc:~# ./out
12345
1
12345
1
12345
12345
12345