cmann
cmann

Reputation: 1980

Problem reading an Integer in java that was written using python’s struct.pack method

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?

Upvotes: 0

Views: 250

Answers (1)

RichieHindle
RichieHindle

Reputation: 281765

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)

Upvotes: 7

Related Questions