| | |
Pascal -> C++; Binary, convert?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2007
Posts: 3
Reputation:
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!
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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:
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:
C++ Syntax (Toggle Plain Text)
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();
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.
> 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.
C++ Syntax (Toggle Plain Text)
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
![]() |
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: Function help/homework
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






