when to use hton/ntoh and when to convert data myself?

前端 未结 2 1974
逝去的感伤
逝去的感伤 2020-12-21 04:23

to convert a byte array from another machine which is big-endian, we can use:

long long convert(unsigned char data[]) {
  long long res;
  res = 0;
  for( in         


        
2条回答
  •  青春惊慌失措
    2020-12-21 05:12

    The hton/ntoh functions convert between network order and host order. If these two are the same (i.e., on big-endian machines) these functions do nothing. So they cannot be portably relied upon to swap endianness. Also, as you pointed out, they are only defined for 16-bit (htons) and 32-bit (htonl) integers; your code can handle up to the sizeof(long long) depending on how DATA_SIZE is set.

提交回复
热议问题