Jump to content
  • Advertisement
Sign in to follow this  
TempHolder

HUGE JAVA/C++ PROBLEM!

This topic is 4698 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites
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. ^^

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
To read an integer, you can read one byte at a time and then assemble them into the integer using bitwise operations.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try writing in XML?

at least it's easy to read and probably not THAT much slower to read in.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!