本文介绍了两个16位整数到一个32位浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能够将modbus中的值提取为16位短路(无符号)或整数(应该被视为16位字)。我的任务是组合两个值,使用java创建一个32位浮点值。
I am able to extract values from modbus as 16-bit shorts (unsigned) or as ints (should be treated as 16-bit words). I am tasked to combine two values to create a single 32 bit float value using java.
我使用gui程序观察到的一些示例值:
some example values I observed using a gui program:
- int + int = float
- 0 + 16256 = 1
- 0 + 17096 = 100
- 0 + 17097 = 100.5
- 0 + 17530 = 1000
- 8192 + 17530 = 1000.5
- int + int = float
- 0 + 16256 = 1
- 0 + 17096 = 100
- 0 + 17097 = 100.5
- 0 + 17530 = 1000
- 8192 + 17530 = 1000.5
我尝试了有点明智的运营商,但这似乎并没有成功。 让我挠头!
I attempted bit wise operators but that didn't seem to do the trick. leaves me scratching my head!
推荐答案你可以使用 Float.intBitsToFloat(int bits )从 int 的位构建 float 。
You can use Float.intBitsToFloat(int bits) to build a float from the bits of an int.
short high = ... // the high 16 bits short low = ... // the low 16 bits int combined = (high << 16) | low; float num = Float.intBitsToFloat(combined);例如:
short high = 17530; short low = 8192;产生浮动 1000.5 。
更多推荐
两个16位整数到一个32位浮点值
发布评论