Binary question..
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]
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?
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 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.
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) );
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) );
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.
Thanks guys.
<< is right shift correct?
if it was used one time on the binary value 10011001 the result would 01001100 right?
<< is right shift correct?
if it was used one time on the binary value 10011001 the result would 01001100 right?
<< left shift.
>> right shift.
Yes this part is correct, for a right 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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement