944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4487
  • C++ RSS
May 4th, 2007
0

Pascal -> C++; Binary, convert?

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Radons is offline Offline
3 posts
since May 2007
May 4th, 2007
0

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

My Pascal is a tad rusty, but I'm reasonably sure the C++ translation is thus:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 5th, 2007
0

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

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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Radons is offline Offline
3 posts
since May 2007
May 6th, 2007
0

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

is the write order of the bytes the same as the read order?
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
May 6th, 2007
0

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

> 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.

C++ Syntax (Toggle Plain Text)
  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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 6th, 2007
0

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

Portability?

I thought it was ease of maintenance? ;-)
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
May 7th, 2007
0

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

Thanks for putting me on the right path, I managed to create some code that did what I wanted. :-)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Radons is offline Offline
3 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: STL, istream_iterator class doesn't compileq
Next Thread in C++ Forum Timeline: Function help/homework





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC