Pascal -> C++; Binary, convert?

Thread Solved
Reply

Join Date: May 2007
Posts: 3
Reputation: Radons is an unknown quantity at this point 
Solved Threads: 0
Radons Radons is offline Offline
Newbie Poster

Pascal -> C++; Binary, convert?

 
0
  #1
May 4th, 2007
Hi! I hope some one is familiar with pascal. I've been trying to translate this code into c++ for quite a while now... And frankly I've given up hope of solving it. It's supposed to invert an integer from binary data it reads from a file into normal characters or numbers.

  1. function gPacker.InvertInteger(_i: uint32): uint32;
  2. begin
  3. Result := (_i and $ff) shl 24;
  4. _i := _i shr 8;
  5. Result := Result + ((_i and $ff) shl 16);
  6. _i := _i shr 8;
  7. Result := Result + ((_i and $ff) shl 8);
  8. _i := _i shr 8;
  9. Result := Result + _i;
  10. end;

The only thing I don't really understand is the first line, how can you shift right without giving a start variable?
Also when I translate for example this line:

_i := _i shr 8;

to

i = i >> 8;

My compiler says "That statement has no effect." Any idea's?

Hope someone can help me out here, though my post might by a bit confusing. Anyway, thanks a bunch!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Pascal -> C++; Binary, convert?

 
0
  #2
May 4th, 2007
My Pascal is a tad rusty, but I'm reasonably sure the C++ translation is thus:
  1. void gPacker::InvertInteger ( unsigned int i )
  2. {
  3. Result = ( i & 0xff ) << 24;
  4. i >>= 8;
  5. Result += ( i & 0xff ) << 16;
  6. i >>= 8;
  7. Result += ( i & 0xff ) << 8;
  8. i >>= 8;
  9. Result += i;
  10. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 3
Reputation: Radons is an unknown quantity at this point 
Solved Threads: 0
Radons Radons is offline Offline
Newbie Poster

Re: Pascal -> C++; Binary, convert?

 
0
  #3
May 5th, 2007
It compiles, thanks. But it doesn't really seem to work, so let me just ask another question if I may. :-)

I'm using an ifstream to read from an archive file, like this:

  1. archive.open(filename, ios::in | ios::ate | ios::binary);
  2.  
  3. size = archive.tellg();
  4. archive.seekg (0, ios::beg);
  5.  
  6. archive.readsome(reinterpret_cast<char*>(&buffer), 4);
  7. somedata = buffer;
  8.  
  9. archive.close();
Sometimes it works, but sometimes it gives some strange results.

Example:

I read 4 bytes with readsome, and the result is: 872808448
When I check those 4 bytes on the file with a hex editor I see: 00 00 06 34 (Should be 1588)

And when I shift 24 bits to the right I get: 52... Which I can figure out why if you take only the 34 from the 00 00 06 34, you get 52. (00 00 00 34)

I have no idea how this works!? Thanks alot!
Last edited by Radons; May 5th, 2007 at 7:21 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,145
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Pascal -> C++; Binary, convert?

 
0
  #4
May 6th, 2007
is the write order of the bytes the same as the read order?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Pascal -> C++; Binary, convert?

 
0
  #5
May 6th, 2007
> 00 00 06 34 (Should be 1588)
But 34 06 00 00 is 872808448
In other words, the order is completely reversed.

http://en.wikipedia.org/wiki/Endian

If you're trying to read a binary value from a file, and the endian of your machine is different to the endian of the machine which wrote the file, then you have to be careful about how you read the data. You can't just dump several bytes directly into a variable.

  1. unsigned long result = 0;
  2. for ( int i = 0 ; i < 4 ; i++ ) {
  3. unsigned char temp;
  4. archive.readsome(reinterpret_cast<char*>(&temp), 1);
  5. // value in file is stored big endian
  6. result = ( result << 8 ) | temp;
  7. }
  8. // result now contains the correct value
  9. // irrespective of the endian on your machine.

Even the use of 4 and 8 can be replaced by suitable constants for extra super portability
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pascal -> C++; Binary, convert?

 
0
  #6
May 6th, 2007
Portability?

I thought it was ease of maintenance? ;-)
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 3
Reputation: Radons is an unknown quantity at this point 
Solved Threads: 0
Radons Radons is offline Offline
Newbie Poster

Re: Pascal -> C++; Binary, convert?

 
0
  #7
May 7th, 2007
Thanks for putting me on the right path, I managed to create some code that did what I wanted. :-)
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC