简体   繁体   English

JAVA /如何将2字节浮点数转换为双精度值

[英]JAVA / How to convert 2-byte float to double value

I am trying to convert 2 byte float value which is 0D 82 to double value on my project. 我试图将0D 82浮点值0D 82为我的项目中的双精度值。

When I simulated that value I get the 28.2 . 当我模拟该值时,得到28.2 So how can I convert that 2 byte float value to double value on java ? 那么如何在Java上将2个字节的float值转换为double值呢?

Thanks for any help ! 谢谢你的帮助 !

Look at the following library: 看下面的库:

https://sourceforge.net/p/calimero/wiki/Home/ https://sourceforge.net/p/calimero/wiki/Home/

It provides a Java API to the KNX network and its data types. 它为KNX网络及其数据类型提供了Java API。 This will most likely do what you want - specfically the tuwien.auto.calimero.dptxlator.DPTXlator2ByteFloat class. 这很可能会完成您想要的操作-特别是tuwien.auto.calimero.dptxlator.DPTXlator2ByteFloat类。

You can use this code: 您可以使用以下代码:

byte[] test = new byte[2];
test[0] = (byte)0x0D;
test[1] = (byte)0x82;

DPTXlator2ByteFloat floatTranslator = new DPTXlator2ByteFloat(DPTXlator2ByteFloat.DPT_AIR_PRESSURE);
floatTranslator.setData(test);
double value = (double)floatTranslator.getValueFloat();

the value variable will have the 28.2 value as expected. value变量将具有预期的28.2值。

Import the following in your maven dependencies to include the library: 在您的maven依赖项中导入以下内容以包括该库:

<dependency>
    <groupId>com.github.calimero</groupId>
    <artifactId>calimero-core</artifactId>
    <version>2.2-beta</version>
<dependency>

Hope this helps. 希望这可以帮助。

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

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