When and how to use the C ++ htonl function

cout << "Hello World !" << endl; 

For my first stack message, overflow : when should we use the htonl function? I looked at the manual page. However, I do not understand when and how to use it.

+1
c ++ htonl
May 22, '15 at 0:59
source share
1 answer

Host to n network translation. This ensures that the endian 32-bit data value is correct (large end) for network transport. ntohl - N etwork TO H ost - is used by the receiver to ensure that endian is correct for the receiver's CPU. Watch out for htons and ntohs for handling 16 bits, and somewhere there are probable htonll and ntohll for 64 bits.

Using all of them is as simple as passing the number you want to convert, and the output is the converted number. You may find that absolutely nothing happened on some processors, because their endian is already big.

 uint32_t inval = 0xAABBCCDD; uint32_t outval = htonl(inval); 

Will it be found on most desktop hardware that outval is set to 0xDDCCBBAA

+2
May 22 '15 at 1:12
source share



All Articles