Java:浮动字节/整数 [英] Java: Bytes to floats / ints

查看:167
本文介绍了Java:浮动字节/整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从x平面通过UDP发送的字节数组。字节(4)都是浮点数或整数...
我试图将它们转换为浮点数,但没有运气到目前为止...



示例数组:
字节数据[41] = {-66,30,73,0};



如何将4个字节转换为int或float,不使用8字节?

解决方案

我不知道 endianness 。 @ dogbane的解决方案可能工作。否则,你基本上需要根据字节的顺序得到字节为一个int类型,例如:

  int asInt = (字节[0]& 0xFF)
| ((bytes [1]& 0xFF)<<< 8)
| ((bytes [2]& 0xFF)<< 16)
| ((bytes [3]& 0xFF)<< 24);

然后,您可以使用以下命令转换为浮点值:

  float asFloat = Float.intBitsToFloat(asInt); 

这基本上是 DataInputStream



编辑 - 按位或



要求澄清在这种情况下,按位OR。虽然这是一个更大的话题,可以更好地独立研究,我会给一个快速的简介。或者( | )是一个按位运算符,其结果是通过单独或从两个操作数中的每个位的位集合。



例如(二进制)

  10100000 
| 10001100
-----------
10101100

当我建议使用它,它涉及到将每个字节移入int中的唯一位置。所以,如果你有字节 {0x01,0x02,0x03,0x04} ,在二进制是 {00000001,00000010,00000011,00000100} code>,您有:

  0000 0001(1)
0000 0010(2 < ; 8)
0000 0011(3 <16)
| 0000 0100(4 << 24)
------------------------------------ --------------------
0000 0100 0000 0011 0000 0010 0000 0001(67 305 985)

当您将两个数字组合在一起并且知道两个中都没有设置两个相应的位请参阅


I have a byte array sent via UDP from x-plane. The bytes (4) are all floats or integers… I tried to cast them to floats but no luck so far…

Example array: byte data[41] = {-66,30,73,0};

How do I convert 4 bytes into int or float and doesn't float use 8 bytes?

解决方案

I don't know the endianness of your data. @dogbane's solution might work. Otherwise, you basically need to get the bytes into an int type depending on the order of the bytes, e.g.:

int asInt = (bytes[0] & 0xFF) 
            | ((bytes[1] & 0xFF) << 8) 
            | ((bytes[2] & 0xFF) << 16) 
            | ((bytes[3] & 0xFF) << 24);

Then you can transform to a float using this:

float asFloat = Float.intBitsToFloat(asInt);

This is basically what DataInputStream does under the covers, but it assumes your bytes are in a certain order.

Edit - On Bitwise OR

The OP asked for clarification on what bitwise OR does in this case. While this is a larger topic that might be better researched independently, I'll give a quick brief. Or (|) is a bitwise operator whose result is the set of bits by individually or-ing each bit from the two operands.

E.g. (in binary)

   10100000
|  10001100
-----------
   10101100

When I suggest using it above, it involves shifting each byte into a unique position in the int. So if you had the bytes {0x01, 0x02, 0x03, 0x04}, which in binary is {00000001, 00000010, 00000011, 00000100}, you have this:

                                  0000 0001   (1)
                        0000 0010             (2 <<  8)
              0000 0011                       (3 << 16)
  | 0000 0100                                 (4 << 24)
  --------------------------------------------------------
    0000 0100 0000 0011 0000 0010 0000 0001   (67 305 985)

When you OR two numbers together and you know that no two corresponding bits are set in both (as is the case here), bitwise OR is the same as addition.

See Also

这篇关于Java:浮动字节/整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆