Reversal of bytes when reading int from binary file!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 18
Reputation: hmortensen is an unknown quantity at this point 
Solved Threads: 2
hmortensen's Avatar
hmortensen hmortensen is offline Offline
Newbie Poster

Reversal of bytes when reading int from binary file!

 
0
  #1
Oct 22nd, 2009
Hi all,

Im trying to read an int from a binary file.. (As I have done many times before)

  1. int chunkLength=0;
  2. 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 345
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 51
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
1
  #2
Oct 22nd, 2009
its because of the endianness. Read about Little endian and big endian theory.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 18
Reputation: hmortensen is an unknown quantity at this point 
Solved Threads: 2
hmortensen's Avatar
hmortensen hmortensen is offline Offline
Newbie Poster
 
0
  #3
Oct 22nd, 2009
Originally Posted by dkalita View Post
its because of the endianness. Read about Little endian and big endian theory.
Ok, so and int (and every other datatype) can be represented in two ways, LSB left or right.

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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,361
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
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.
"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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 18
Reputation: hmortensen is an unknown quantity at this point 
Solved Threads: 2
hmortensen's Avatar
hmortensen hmortensen is offline Offline
Newbie Poster
 
0
  #5
Oct 22nd, 2009
Originally Posted by Dave Sinkula View Post
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.
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??

Thanks for the links tho,
- Hmortensen
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,361
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #6
Oct 22nd, 2009
Originally Posted by hmortensen View Post
As I wrote in the original questinon, I do know what I got.. Its a PNG file! Sooo, text file is not an option.
Hm. I don't see the mention -- perhaps it's my eyesight. But no matter. Clarification achieved.

Originally Posted by hmortensen View Post
And I do know how to test and swap. But the question now is where/how does the compiler differentiate in the two projects??
I don't think I'd call it a compiler issue. I would think that perhaps there oughtta be a way to test the file for its endianness and handle then integers appropraitely.

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).
So I guess I'd choose 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 18
Reputation: hmortensen is an unknown quantity at this point 
Solved Threads: 2
hmortensen's Avatar
hmortensen hmortensen is offline Offline
Newbie Poster
 
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC