I'm currently trying to read a file as hex value, like a hex editor do. For the explanation of the issue let's suppose I've a test.txt
with a simple "Hello world" inside. I'm trying to read as hex with a program close to this following code.
#include <iostream>
#include <fstream>
int main(int argc, char* argv[]) {
std::ifstream stream;
stream.open("test.txt", std::ios_base::binary);
if (!stream.bad()) {
std::cout << std::hex;
std::cout.width(2);
while (!stream.eof()) {
unsigned char c;
stream >> c;
std::cout << static_cast<unsigned>(c) << " ";
}
}
return 0;
}
As output in the terminal I got
nux@pc-lubuntu:~/repos/readingHex$ od -x test.txt
0000000 6548 6c6c 206f 6f77 6c72 0a64
0000014
nux@pc-lubuntu:~/repos/readingHex$ ./a.out
48 65 6c 6c 6f 77 6f 72 6c 64 64
Obviously, there's a difference of endian, but that's should be easy to correct. However, as you can see on the output log, results are different at byte 5-6 and 9-10. Do someone have any idea to fix this ?
Thank you.