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!

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;
}

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!

is the write order of the bytes the same as the read order?

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

Portability?

I thought it was ease of maintenance? ;-)

Thanks for putting me on the right path, I managed to create some code that did what I wanted. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.