I have this data file in hexadecimal and I want to convert them to big endian from little endian but I come across this trouble in C++. C++ treats 2 hexa value as one to make a byte for char and always outputs decimal equivalent to those 2 combined hexa value but I don't want this.
Say I am reading a date, 8 byte in little endian:
e218 70d2 0e00 0000
Code:
char buffer;
for(int i = 0; i<BYTE_SIZE; ++i){
infile>>buffer;
cout<<"Read In Int = "<<(unsigned int)buffer<<endl;
cout<<"Read as Hex= "<<hex<<(int)buffer<<endl;
}
Output:
Read as Int= 226
Read as Hex= e2
-------------End of First Iter-------------------------
Read as Int= 18
Read as Hex= 18
...
So if you note that e2, which is in base 16, is concatenated as one entity. When you convert to int, you get: 14*16 + 2 = 226. I want to read e only and then 2 only and so on.... My main goal is to flip it and read it as 18e2 and convert that to decimal. How can I do this?
Update: I think people are misunderstanding this question. I simply wanted a way to read binary file containing hexa values and shift things around individually. This problem was hard to describe on text but anyway, Thank you to those who commented with possible solutions.