| | |
Reversal of bytes when reading int from binary file!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hi all,
Im trying to read an int from a binary file.. (As I have done many times before)
I know (and can see) that the file contains the bytes 00 00 00 0D, so to the best of my knowledge the int should be 13 dec after the read.
BUT it contains 0D 00 00 00 or 218103808 in dec..
Why/how does this reversal happen????
Thanks for any comment on this small issue.
- Hmortensen
Im trying to read an int from a binary file.. (As I have done many times before)
C++ Syntax (Toggle Plain Text)
int chunkLength=0; fileRead.read (reinterpret_cast<char*>(&chunkLength),sizeof(chunkLength));
I know (and can see) that the file contains the bytes 00 00 00 0D, so to the best of my knowledge the int should be 13 dec after the read.
BUT it contains 0D 00 00 00 or 218103808 in dec..
Why/how does this reversal happen????
Thanks for any comment on this small issue.
- Hmortensen
0
#3 Oct 22nd, 2009
•
•
•
•
its because of the endianness. Read about Little endian and big endian theory.
But my additional question now is (google didn't help on this one).. Is this something I can force my compiler to handle for me? I normally use VC++ express, and have before used the same snip to read from binary files without problems, but this time I started with an empty project. So are there a definition, header og property I can set/include, to ensure that I always have LSB to the "right".
Thanks
- Hmortensen
2
#4 Oct 22nd, 2009
You kinda need to know whether the binary contains LE or BE data.
Some somewhat related FAQs:
http://c-faq.com/misc/endiantest.html
http://c-faq.com/cpp/ifendian.html
http://c-faq.com/misc/byteswap.html
Or perhaps consider a text file.
Some somewhat related FAQs:
http://c-faq.com/misc/endiantest.html
http://c-faq.com/cpp/ifendian.html
http://c-faq.com/misc/byteswap.html
Or perhaps consider a text file.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
0
#5 Oct 22nd, 2009
•
•
•
•
You kinda need to know whether the binary contains LE or BE data.
Some somewhat related FAQs:
http://c-faq.com/misc/endiantest.html
http://c-faq.com/cpp/ifendian.html
http://c-faq.com/misc/byteswap.html
Or perhaps consider a text file.
And I do know how to test and swap. But the question now is where/how does the compiler differentiate in the two projects??Thanks for the links tho,
- Hmortensen
0
#6 Oct 22nd, 2009
•
•
•
•
As I wrote in the original questinon, I do know what I got.. Its a PNG file! Sooo, text file is not an option.
•
•
•
•
And I do know how to test and swap. But the question now is where/how does the compiler differentiate in the two projects??
Hmm... interesting.
http://en.wikipedia.org/wiki/Portabl...hnical_details
[edit]Or don't mind me, I'm just playing along at an entirely different speed, I guess. :p
•
•
•
•
8.1. Chunks
Chunks were designed to be easily tested and manipulated by computer programs, easily detected by human eyes, and reasonably self-contained. Every chunk has the same structure: a 4-byte length (in "big-endian" format, as with all integer values in PNG streams), a 4-byte chunk type, between 0 and 2,147,483,647 bytes of chunk data, and a 4-byte cyclic redundancy check value (CRC).
ntohs , htons , ntohl , and htonl for starters. Last edited by Dave Sinkula; Oct 22nd, 2009 at 12:35 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
0
#7 Oct 23rd, 2009
Thanks for all your help guys,
As it turned out all my problems arose, as the first project i did was with TGA files, where the header information is in little-endian format, so ofcourse it worked. And then when working with PNG files where the header chunk is in big-endian, it didn't.
So I was focokused on the different project styles (with and without precompiled headers), and not the format of the information in the file.
Thanks again, yet anothe aspect of programming I never knew, is now known.
- Hmortensen
As it turned out all my problems arose, as the first project i did was with TGA files, where the header information is in little-endian format, so ofcourse it worked. And then when working with PNG files where the header chunk is in big-endian, it didn't.
So I was focokused on the different project styles (with and without precompiled headers), and not the format of the information in the file.
Thanks again, yet anothe aspect of programming I never knew, is now known.

- Hmortensen
![]() |
Similar Threads
- First step in reading a text file and converting it to a binary file (C++)
- Error when reading a binary file! (C++)
- Problems reading a Binary File more than 2.0 GB with VC++ (C++)
- Reading bytes from binary file (Visual Basic 4 / 5 / 6)
- fscanf and reading from binary file (C)
- Binary File Parser (C++)
- reading ints from a binary file (C)
- Reading a Binary File to a C++ Class (C++)
Other Threads in the C++ Forum
- Previous Thread: Visual C++ OPC client
- Next Thread: Directory operation
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






