lundi 2 septembre 2019

Why the value reverse as a unit of 8 bytes when dereferencing a pointer?

I am trying to deal with the data in MINST. However, an unsigned int(unit_t32) pointer was pointed to 0x00000803, but after dereferencing it, the value was 0x03080000 in hexadecimal form. ( the debugger shows that the content of the pointer is 0x00000803, however, after dereferencing, the value is 0x03080000)

I am running it on Xcode15.0; In the past, I thought it was because of the ' space ' between every 2 hexadecimal number. But it showed the same result after delete the "space" in the data file. Then I checked the memory, and its content is 0x00000803 00000005.... The value should be 0x0803, but it is 0x03080000. I am really confused.

unsigned long read_header(const std::unique_ptr& buffer, size_t position) {

auto header = reinterpret_cast<uint32_t*>(buffer.get());
auto value = *(header + position);

return (value << 24) | ((value << 8) & 0x00FF0000) | ((value >> 8) & 0X0000FF00) | (value >> 24);

}

Aucun commentaire:

Enregistrer un commentaire