Reading “integer” size bytes from a char* array.

前端 未结 9 1292
深忆病人
深忆病人 2020-12-09 05:13

I want to read sizeof(int) bytes from a char* array.

a) In what scenario\'s do we need to worry if endianness needs to be checked?

9条回答
  •  不知归路
    2020-12-09 05:16

    Why read when you can just compare?

    bool AreEqual(int i, char *data)
    {
       return memcmp(&i, data, sizeof(int)) == 0;
    }
    

    If you are worrying about endianness when you need to convert all of integers to some invariant form. htonl and ntohl are good examples.

提交回复
热议问题