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

前端 未结 9 1276
深忆病人
深忆病人 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:33

    You shouldn't need to worry about endianess unless you are reading the bytes from a source created on a different machine, e.g. a network stream.

    Given that, can't you just use a for loop?

    void ReadBytes(char * stream) {
        for (int i = 0; i < sizeof(int); i++) {
            char foo = stream[i];
            }
        }
     }
    

    Are you asking for something more complicated than that?

提交回复
热议问题