r/learncpp • • Aug 23 '21
endianness
When writing numbers to files using ofstream, on my little endian machine those numbers are written in little endian, but I assume that for big endian machines those numbers are written in big endian, I want to be certain ofstream doesn't handle endianness for me. Also how do you suggest I safely write to files?
4
Upvotes
View all comments
Show parent comment
3
u/HappyFruitTree Aug 23 '21
It depends on what you mean by "written as 0x1234". Types such as unit16_t does not exists in the data file. It's just a sequence of bytes.
If you were to read the value back on a computer with the same endianness you would get the same value, but if the endianness is different the bytes would be in the wrong order and you would instead get the value 0x3412.