User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,272 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,421 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2518 | Replies: 6 | Solved
Reply
Join Date: May 2007
Posts: 3
Reputation: Radons is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Radons Radons is offline Offline
Newbie Poster

Pascal -> C++; Binary, convert?

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

function gPacker.InvertInteger(_i: uint32): uint32;
begin
   Result := (_i and $ff) shl 24;
   _i := _i shr 8;
   Result := Result + ((_i and $ff) shl 16);
   _i := _i shr 8;
   Result := Result + ((_i and $ff) shl 8);
   _i := _i shr 8;
   Result := Result + _i;
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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 487
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

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

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

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

  #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:

archive.open(filename, ios::in | ios::ate | ios::binary);

size = archive.tellg();
archive.seekg (0, ios::beg);

archive.readsome(reinterpret_cast<char*>(&buffer), 4);
somedata = buffer;

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  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

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

  #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  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

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

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

unsigned long result = 0;
for ( int i = 0 ; i < 4 ; i++ ) {
    unsigned char temp;
    archive.readsome(reinterpret_cast<char*>(&temp), 1);
    // value in file is stored big endian
    result = ( result << 8 ) | temp;
}
// result now contains the correct value
// 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  
Join Date: Jun 2006
Location: India
Posts: 7,012
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

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

  #6  
May 6th, 2007
Portability?

I thought it was ease of maintenance? ;-)
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: May 2007
Posts: 3
Reputation: Radons is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Radons Radons is offline Offline
Newbie Poster

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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:25 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC