【发布时间】:2015-03-13 18:11:47
【问题描述】:
如何在java中将十进制值(温度)转换为16位十六进制?
输入:-54.9
预期结果:0x8225
我有反向代码,我将 16 字节十六进制转换为十进制值(温度)。
private static double hexDataToTemperature(String tempHexData) {
String tempMSBstr = tempHexData.substring(0, 2);
String tempLSBstr = tempHexData.substring(2, 4);
int tempMSB = Integer.parseInt(tempMSBstr, 16);
int tempLSB = Integer.parseInt(tempLSBstr, 16);
int sign = 1;
if (tempMSB >= 128) {
tempMSB = tempMSB - 128;
sign = -1;
}
Float f = (float) (sign * ((float) ((tempMSB * 256) + tempLSB) / 10));
return Double.parseDouble("" + f);
}
【问题讨论】:
-
首先是在stackoverflow数据库中搜索它是个好主意
-
@Stultuske 在这里我们应该使用 Java 提供的东西,所以这个特定的链接没有帮助。
-
我认为这行不通,因为我需要将 -25.5 之类的值转换为十六进制。
-
@laune:我编辑了它。
标签: java hex decimal data-conversion