Little endian - Big endian in c socket | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Little endian - Big endian in c socket

When i usally send data i used htons to convert from little to big endian no problrm there but now my data is this uint8_t *arr; Do i need to use anything, is it obsuletr since i am using bytes in which little endian and big endian are same

21st Jul 2020, 8:44 AM
Eren
Eren - avatar
1 Answer
+ 2
You are thinking correctly. The terms Little and Big Endian describe byte order for multi-byte data types. Since unit8_t is only one byte, there is no concern about potential for byte order to cause a mismatch in data element interpretation. There is a clue in the function prototype for htons() that clarifies whether it would be appropriate to use in this case: uint16_t htons(uint16_t hostshort); Its input parameter is defined as uint16_t, so it would be wrong to pass in uint8_t. And the output that it returns is unint16_t as well, so assigning it to a uint8_t could cause loss of data on a little endian machine. (It would copy the MSB, which would be 0, into the uint8_t byte).
23rd Jul 2020, 10:24 PM
Brian
Brian - avatar