0

First I write the integer using python:
out.write( struct.pack(">i", int(i)) );

I then read the integer using DataInputStream.readInt() in Java.
I works but when it tries to read the number 10, and probably some other numbers too, it starts to read garbage.
Reading the numbers:
0, 4, 5, 0, 5, 13, 10, 1, 5, 6
Java reads:
0, 4, 5, 0, 5, 13, 167772160, 16777216, 83886080

What am I doing wrong?

1 Answer 1

7

Psychic debugging: You're writing the output in text mode on Windows using code like this:

f = open("output.dat", "w")
f.write(my_data)

and that's making your 13 (which is a newline) become carriage return / newline (10, 13).

You need to write your output in binary mode:

f = open("output.dat", "wb")
f.write(my_data)
Sign up to request clarification or add additional context in comments.

1 Comment

Of course! You have to write in binary mode to write binary data. That didn't even cross my mind. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.