繁体   English   中英

Python Struct和读取Java中的值

[英]Python Struct and reading values in Java

我想导出二进制格式,然后用Java读取二进制,但是例如,我无法获得正确的值

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

在Java中,我有这个值: 8.962863E27

我尝试发送一个二进制文件并将输出匹配到用Java编写的ubjson库,起初我使用Big-endian标记,但是不起作用,当我使用Little-endian时,它的工作原理是这样的。

感谢您的指导。

编辑:库的某些部分

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') 
                .....

您的Java应用程序使用相反的极性。 您正在编写little-endian,但是Java将值解释为big-endian:

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

编写big-endian,Java将正确读取值:

struct.pack('>f', 21.988)

如果这不起作用,还有其他原因导致您的输出未正确解释。 UBJSON规范对字节顺序非常清楚,应该全部使用big-endian进行编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM