Issue
The variable 'value' is uint32_t
value = htonl(value);
printf("after htonl is %ld\n\n",value);
This prints -201261056
value = htons(value);
printf("after htons is %ld\n\n",value);
This prints 62465
Please suggest what could be the reason?
Solution
I guess your input is 500, isn't it?
500 is 2**8+2**7+2**6+2**5+2**4+2**2
or 0x00 0x00 0x01 0xF4
in little endian order.
TCP/IP uses big endian. So after the htonl, the sequence is 0xF4 0x01 0x00 0x00
.
If you print it as signed integer, since the first digit is 1, it is negative then. Negative numbers are regarded as complement, The value is -(2**27 + 2**25+2**24+2**23+2**22+2**21+2**20+2**19+2**18+2**17+2**16)
== -201261056
Answered By - halfelf Answer Checked By - Terry (WPSolving Volunteer)