Java从InputStream读取32位浮点数的不同方法

huangapple 未分类评论57阅读模式
英文:

Java Different Methods Reading 32 Bit Float From InputStream

问题

我在从API接收到的InputStream中读取32位浮点值时遇到了问题。

API文档中描述的文件格式如下:

> 宽度 x 高度 x 帧数 32位浮点压力值,按帧存储,按行主序方式排列

我目前是这样读取浮点值的:

DataInputStream dis = new DataInputStream(is);

while (dis.available() > 0) {
    byte[] float_val = new byte[4];
    dis.readFully(float_val);
    float f = ByteBuffer.wrap(float_val).order(ByteOrder.LITTLE_ENDIAN).getFloat();
    System.out.println(f);
}

由于文档中没有提到字节顺序,我假设它是小端字节顺序,因为我也测试过大端字节顺序,但从中获得的值太混乱,无法有效使用。然而,当我使用小端字节顺序显示这些值时,我的同事(需要从InputStream获取值的同事)指出我打印出的值在使用方面“过低”。我现在想问,你们是否有任何关于在Java中另一种读取32位浮点值的方法的想法?

英文:

I have a problem reading 32Bit Float Values from an InputStream recieved from an API.

The File-Format ist described in the API Documentation as:

> width x height x nbFrames 32-bit floating point pressure values, stored frame by frame and in row-major fashion

I´m currently Reading the Float Values like this

 DataInputStream dis = new DataInputStream(is);

 while (dis.available() > 0) {

        byte[] float_val = new byte[4];
        dis.readFully(float_val);
        float f = ByteBuffer.wrap(float_val).order(ByteOrder.LITTLE_ENDIAN).getFloat();
        System.out.println(f);

   }

Since the ByteOrder is not mentioned in the Documentation i assumed it will be Little Endian since I tested with Big Endian aswell but the Value i recived from this where way too messy to be valid.
However, when I showed the Values with Little Endian Byteorder my Coworker who needs the Values from the InputStream he pointed out the values I get printed are "too low" in the sense of usage.
I´m asking now do you guys have any Idea for another approach reading 32 Bit Float Values in Java?

huangapple
  • 本文由 发表于 2020年3月16日 00:14:22
  • 转载请务必保留本文链接:https://java.coder-hub.com/60694948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定