HUGE JAVA/C++ PROBLEM!

Started by
22 comments, last by Enigma 18 years, 6 months ago
Hello I am in desperate need of help. I created a file format in Java using its DataOutputStream, this writes a map format I made, and it starts of with a integer that holds the number of floorpanels. When I save the work in the application I made, I can load the data back up easily using DataInputStream and it gives me the correct data I want. The problem is the format is to be used in a C++ Application. When I use fstream to read the binary data from the Java Programs data, it doesnt read right. I try to first read the integer I saved which is the very first thing in the file with fstream and i get 50331648, when it should be equal to 4. I was wondering if anyone knows what the problem might be?, My code works when I try to load the file with the Java Application itself, and my C++ code works when I tested it making a file in C++. If anyone can help me it would be greatly appreciated, possibly a sample source code of this same problem. TY!
Advertisement
50331648 in binary = 0000 0011 0000 0000 0000 0000 0000 0000

if we reverse the bytes, this becomes 0000 0000 0000 0000 0000 0000 0000 0011

But that is 3, which means it is still off by one...still, I can't help but think that byte order is part of the problem. Note that DataOutputStream uses the same byte order on any machine, and this byte order might not match your machine's native order, so for platform independence you should define the byte order in your map file as well. Also are you sure the integer is 4 and not 3?
Download a hex editor and go to town on the files you write. The easiest and best answers to file questions can be found in the actual file itself. Writing programs which expect certain kinds of values, and certain kinds of byte order, and certain kinds of alignments just cause headaches more often than not. ^^
Read the specification of DataOutputStream. It says, for example:

void writeInt(int v)
Writes an int to the underlying output stream as four bytes, high byte first.

It works backwards.
hey it is suppose to be 3 my bad, so how could i convert that into a the acutal number 3 in C++?

And also what would be best to fix this problem with Java or C++?, if so tell me how I might fix it with whichever.
To read an integer, you can read one byte at a time and then assemble them into the integer using bitwise operations.

Quote:Original post by TempHolder
hey it is suppose to be 3 my bad, so how could i convert that into a the acutal number 3 in C++?

And also what would be best to fix this problem with Java or C++?, if so tell me how I might fix it with whichever.


Use std::bitset?
Try writing in XML?

at least it's easy to read and probably not THAT much slower to read in.
Warning: Completely Non-Cross-Platform solution

int a;
a << your_stream;
_asm mov eax, a
_asm bswap eax
_asm mov a, eax

EDIT: x86 only, xml is a far better solution.
the loading speed doesnt matter much to me, and I dont know anything about XML so what should i go with?

This topic is closed to new replies.

Advertisement