•
•
•
•
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
![]() |
•
•
Join Date: May 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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!
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;
} I'm here to prove you wrong.
•
•
Join Date: May 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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:
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!
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.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 19
Solved Threads: 200
> 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.
Even the use of 4 and 8 can be replaced by suitable constants for extra super portability
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- binary counter (Visual Basic 4 / 5 / 6)
- Real number to binary? (C)
- ASCII to BINARY, & VICA VERCA (C++)
Other Threads in the C++ Forum
- Previous Thread: STL, istream_iterator class doesn't compileq
- Next Thread: BGI graphics not supported in windows..



Linear Mode