0

I want to export a binary format and then I read the binary in Java, But i am not able to get correct values, for example

f.write(struct.pack('<f', 21.988))

in Java I have this value: 8.962863E27

I try to send a binary and match the output to ubjson library written in java, at first I use Big-endian mark but does not work, and when I use Little-endian it works like that.

Thanks for any guides.

Edit: some part of library

public JsonValue parse(final DataInputStream din) throws IOException {
        return parse(din, din.readByte());
    }

    protected JsonValue parse(final DataInputStream din, final byte type) throws IOException {
        if (type == '[')
            return parseArray(din);
        else if (type == '{')
            return parseObject(din);
        else if (type == 'Z')
            return new JsonValue(JsonValue.ValueType.nullValue);
        else if (type == 'T')
            return new JsonValue(true);
        else if (type == 'F') 
                .....
4
  • Have you looked at the file encoding to ensure that it's reading both files in the exact same format? Commented Nov 12, 2013 at 17:43
  • Mike I edited Post with part of library, it uses default Java Streams and read bytes. Commented Nov 12, 2013 at 17:50
  • and you're opening the file in python as 'wb' correct? Which is binary mode in python (only required on windows - but should be left in for cross platform support). Commented Nov 12, 2013 at 17:59
  • thanks, but I export to Java, as @martin wrote it was problem of Big/little-endian. Commented Nov 12, 2013 at 18:04

1 Answer 1

1

Your Java application is using the opposite endianess; you are writing little-endian, but Java interpreted the value as big endian:

>>> struct.unpack('>f', struct.pack('<f', 21.988))
(8.962863280123082e+27,)

Write big-endian and Java will read the values correctly:

struct.pack('>f', 21.988)

If that doesn't work there are other reasons that your output is not interpreted correctly. The UBJSON specification is quite clear about endianess, it should all be encoded big-endian.

Sign up to request clarification or add additional context in comments.

7 Comments

@daniel: Then study the source code I linked you to some more. It certainly uses big-endian packing and unpacking.
@martin I studied before, in 3 days researching and tests.
I didn't say you had not. But you'll need to compare your output with that of that library. Switching to little-endianess is not going to solve your problem.
Thanks @martin, you answer was currect, but I do not know other part is still in little-endian but float number in big-endian, and it works, f.write(struct.pack('<s', b"vert")) f.write(struct.pack('<c', b"d")) f.write(struct.pack('>f', 21.988))
Characters are just one byte long, so there is no difference between endianess. struct.pack('<s', b"vert") == struct.pack('>s', b"vert") is True.
|

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.