Java:浮动到十六进制和反向
Java: Float to hex and reverse
嘿嘿,
我有一个小问题。所以我得到了一个测量设备,并通过 modbus 检索数据。我通常通过 "Simply Modbus TCP Client" 检查值,它会显示正确的数据。现在我想自动化这个给我带来麻烦的过程。
我得到了正确的响应,但棘手的部分似乎是转换。我得到了相同的十六进制响应,但是当我转换它时,我不会得到相同的结果。
看看截图
你可以看到左边的响应,右边的字节(4175 AF8A)和对应的值(22739113)。我希望 "bytes" 是十六进制表示,但我无法将它们转换为十进制数。
我编写了一个 java 小程序来显示这些数字及其转换。
public static void main(String[] args) {
HashMap<String, Integer> values = new HashMap<>();
values.put("4175AF52", 22738214);
values.put("41D61FAC", 1484698204);
values.put("419A08A0", 109193237);
Iterator it = values.entrySet().iterator();
while (it.hasNext()) {
System.out.println("/************************************************/");
Map.Entry pair = (Map.Entry) it.next();
int hexIntepretation = (int) Long.parseLong((String) pair.getKey(), 16);
// Print the result
System.out.println(pair.getKey() + " = " + pair.getValue());
System.out.println("Expected:\t" + pair.getValue());
System.out.println("Hex to Int:\t" + hexIntepretation);
System.out.println("Int to Hex:\t" + Integer.toHexString((int) pair.getValue()));
System.out.println("Float to Hex:\t" + Float.toHexString((int) pair.getValue()));
System.out.println("Hex to Int:\t" + Integer.parseInt((String) pair.getKey(), 16));
it.remove(); // avoids a ConcurrentModificationException
}
}
运行 这个片段给出了这个输出:
/*******************************************/
419A08A0 = 109193237
Expected: 109193237
Hex to Int: 1100613792
Int to Hex: 6822815
Float to Hex: 0x1.a08a06p26
Hex to Int: 1100613792
/*******************************************/
41D61FAC = 1484698204
Expected: 1484698204
Hex to Int: 1104551852
Int to Hex: 587eb25c
Float to Hex: 0x1.61facap30
Hex to Int: 1104551852
/*******************************************/
4175AF52 = 22738214
Expected: 22738214
Hex to Int: 1098231634
Int to Hex: 15af526
Float to Hex: 0x1.5af526p24
Hex to Int: 1098231634
无论我做什么,我都无法获取值。但是,当我执行 "float-to-hex" 时,我会在其中找到一些初始值。
示例:
4175AF52 = 22738214
十六进制浮点数:0x1.5af526p24
但我无法将各个部分拼在一起来解决这个难题。感谢您的帮助!
正如您的图片所说的“64 位浮点数”,我会将字节解释为 double
。
https://en.wikipedia.org/wiki/Double-precision_floating-point_format
不幸的是,显示器似乎没有显示浮点值:
int bits = 0x4175_AF8A;
float f = Float.intBitsToFloat(bits);
System.out.println(f);
给出:
15.355356
感谢@Fildor,正确计算:
long nbits = 0x1769_0314_4175_AF8AL;
nbits = 0x4175_AF8A_19ED_F046L;
double d = Double.longBitsToDouble(nbits);
System.out.println(d); // 2.273910562059047E7
上面写着“64 位浮点数”,但 GUI 只显示 32 位。显然视图将其他 4 个字节切掉,但在左侧的字段中,您可以看到它们。
正如您已经提到的,当您从 "bytes" 列复制所有内容时,您还将获得 "missing" 的内容。
嘿嘿,
我有一个小问题。所以我得到了一个测量设备,并通过 modbus 检索数据。我通常通过 "Simply Modbus TCP Client" 检查值,它会显示正确的数据。现在我想自动化这个给我带来麻烦的过程。
我得到了正确的响应,但棘手的部分似乎是转换。我得到了相同的十六进制响应,但是当我转换它时,我不会得到相同的结果。
看看截图
你可以看到左边的响应,右边的字节(4175 AF8A)和对应的值(22739113)。我希望 "bytes" 是十六进制表示,但我无法将它们转换为十进制数。
我编写了一个 java 小程序来显示这些数字及其转换。
public static void main(String[] args) {
HashMap<String, Integer> values = new HashMap<>();
values.put("4175AF52", 22738214);
values.put("41D61FAC", 1484698204);
values.put("419A08A0", 109193237);
Iterator it = values.entrySet().iterator();
while (it.hasNext()) {
System.out.println("/************************************************/");
Map.Entry pair = (Map.Entry) it.next();
int hexIntepretation = (int) Long.parseLong((String) pair.getKey(), 16);
// Print the result
System.out.println(pair.getKey() + " = " + pair.getValue());
System.out.println("Expected:\t" + pair.getValue());
System.out.println("Hex to Int:\t" + hexIntepretation);
System.out.println("Int to Hex:\t" + Integer.toHexString((int) pair.getValue()));
System.out.println("Float to Hex:\t" + Float.toHexString((int) pair.getValue()));
System.out.println("Hex to Int:\t" + Integer.parseInt((String) pair.getKey(), 16));
it.remove(); // avoids a ConcurrentModificationException
}
}
运行 这个片段给出了这个输出:
/*******************************************/
419A08A0 = 109193237
Expected: 109193237
Hex to Int: 1100613792
Int to Hex: 6822815
Float to Hex: 0x1.a08a06p26
Hex to Int: 1100613792
/*******************************************/
41D61FAC = 1484698204
Expected: 1484698204
Hex to Int: 1104551852
Int to Hex: 587eb25c
Float to Hex: 0x1.61facap30
Hex to Int: 1104551852
/*******************************************/
4175AF52 = 22738214
Expected: 22738214
Hex to Int: 1098231634
Int to Hex: 15af526
Float to Hex: 0x1.5af526p24
Hex to Int: 1098231634
无论我做什么,我都无法获取值。但是,当我执行 "float-to-hex" 时,我会在其中找到一些初始值。
示例:
4175AF52 = 22738214
十六进制浮点数:0x1.5af526p24
但我无法将各个部分拼在一起来解决这个难题。感谢您的帮助!
正如您的图片所说的“64 位浮点数”,我会将字节解释为 double
。
https://en.wikipedia.org/wiki/Double-precision_floating-point_format
不幸的是,显示器似乎没有显示浮点值:
int bits = 0x4175_AF8A;
float f = Float.intBitsToFloat(bits);
System.out.println(f);
给出:
15.355356
感谢@Fildor,正确计算:
long nbits = 0x1769_0314_4175_AF8AL;
nbits = 0x4175_AF8A_19ED_F046L;
double d = Double.longBitsToDouble(nbits);
System.out.println(d); // 2.273910562059047E7
上面写着“64 位浮点数”,但 GUI 只显示 32 位。显然视图将其他 4 个字节切掉,但在左侧的字段中,您可以看到它们。
正如您已经提到的,当您从 "bytes" 列复制所有内容时,您还将获得 "missing" 的内容。