两个16位整数到一个32位浮点值

编程入门 行业动态 更新时间:2024-10-25 00:31:15
本文介绍了两个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位浮点值

本文发布于:2023-11-30 01:34:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浮点   整数   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!