Binary question..

Started by
8 comments, last by prh99 21 years ago
I am writting a program that allows me to modify IFF files (archive format used by EA). I think the MSB is in the opposite position. For example, One of the values in the header of the file I testing my program with is suppose to be 0x3228 but I get 0x2832 (the location is stored as a 32 bit integer). I was wondering what the quickest way to flip the bits would be. Thanks Edit: Sorry I made a mistake the values should be 0x28320000 and 0x00003228. I don't know why I forgot the zeros. The file format was developed on Apple so all the numbers are stored in Motorola format. [edited by - prh99 on April 2, 2003 1:55:20 PM]
Patrick
Advertisement
if 3228 gets rewritten to 2832 its either not an int or its just coincedce tjhey are flipped. i think the most likely thing is its stored as a string of four chars.
if it is an integer, but these numbers were just examples, i would read the 2 high and 2 low bytes seperatly as words, and then do something like this: (syntax probably not correct though, just started with c++)

>> low //get words from stream
>> high
myint = ((int)high << 16) | (int)low // << is the bitshift operator in c++, not?
yeaqh it is, I agree. i recon its 16 bits integer, or string manipulation. I can''t see why itwould be shifted that way.

I would definitely say its a 16 bit integer. I doubt it is a string, cuz then it would return asci values, and it wouldnt be null terminated.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
Looks like the files are stored in little-endian format.

If it is a 16bit value, stored in a 16bit short, you can rotate 8 bits to reverse the order...


short myVal = 0x2832;

asm ror myVal, 8; // _asm in VC++, I think.

myVal would now be 0x3228.


If you''re storing them in a 32bit variable, you can read them in one byte at a time...

long myVal = 0;

myVal = (long)( file.get() | (file.get() << 8) );
"I thought Genius lived in bottles..." - Patrick Star
quote:Edit: Sorry I made a mistake the values should be 0x28320000 and 0x00003228. I don''t know why I forgot the zeros. The file format was developed on Apple so all the numbers are stored in Motorola format.



Ok then change that second method I showed to...

long myVal = 0;

myVal = (long)( file.get() | (file.get() << 8) | (file.get() << 16) | (file.get() << 24) );

That should work.
"I thought Genius lived in bottles..." - Patrick Star
Thanks guys.

<< is right shift correct?

if it was used one time on the binary value 10011001 the result would 01001100 right?
Patrick
<< left shift.
>> right shift.


quote:if it was used one time on the binary value 10011001 the result would 01001100 right?


Yes this part is correct, for a right shift.
"I thought Genius lived in bottles..." - Patrick Star
quote:Original post by Xanth
<< left shift.
>> right shift.


if it was used one time on the binary value 10011001 the result would 01001100 right?


Yes this part is correct, for a right shift.

Okay thanks, it works.

Patrick
stupid question maybe, but makes the prefix ''0x'' a number hexadecimal in c++?
quote:Original post by Eelco
stupid question maybe, but makes the prefix '0x' a number hexadecimal in c++?


No it's not stupid. Yes 0x prefix marks a number as hex in C and C++.


[edited by - prh99 on April 2, 2003 2:31:24 PM]
Patrick

This topic is closed to new replies.

Advertisement