简体   繁体   English

字节数组(以十六进制表示)转换为Int问题。 (科特林/爪哇)

[英]Array of Bytes (as hex) conversion to Int issue. (Kotlin/Java)

I'm looking at parsing information from a temp/humidity sensor that was provided with the following instructions; 我正在查看来自温度/湿度传感器的信息,该信息随附以下说明:

There are 6 bytes. 有6个字节。

  1. Temperature positive/negative: 0 means positive (+) and 1 means negative (-) 温度正/负:0表示正(+),1表示负(-)

  2. Integer part of temperature. 温度的整数部分。 Show in Hexadecimal. 以十六进制显示。

  3. Decimal part of temperature. 温度的小数部分。 Show in Hexadecimal. 以十六进制显示。

  4. Reserved byte. 保留字节。 Ignore it. 忽略它。

  5. Integer part of humidity. 湿度的整数部分。 Show in Hexadecimal. 以十六进制显示。

  6. Decimal part of humidity. 湿度的小数部分。 Show in Hexadecimal. 以十六进制显示。

For example: 00 14 05 22 32 08 means +20.5C 50.8% & 01 08 09 00 14 05 means -8.9C 20.5% 例如:00 14 05 22 32 08表示+ 20.5C 50.8%&01 08 09 00 14 05表示-8.9C 20.5%

as each byte is in hex & i need to covert this to an Int I followed this approach - Java code To convert byte to Hexadecimal but the values I get when validating their example don't make sense.In Kotlin I do; 因为每个字节都以十六进制表示,所以我需要将其隐式转换为Int,我遵循这种方法-Java代码将字节转换为十六进制,但是在验证其示例时获得的值没有意义。

val example = byteArrayOf(0, 14, 5, 22, 32, 8)
example.map { Integer.parseInt(String.format("%02X ", it),16)}

First Example output is; 第一个示例输出为;

0 = "00 "
1 = "08 "
2 = "09 "
3 = "00 "
4 = "0E "
5 = "05 "

Second Example output; 第二个示例输出;

0 = "00 "
1 = "0E "
2 = "05 "
3 = "16 "
4 = "20 "
5 = "08 "

What am I doing wrong? 我究竟做错了什么? I'm starting to think the manufactures instructions could be 'misleading' 我开始认为制造商的说明可能会“误导”

A key thing you are missing here is that when passing in the it parameter to String#format , that value is a decimal int , not a hexadecimal int . 这里缺少的关键是,将it参数传递给String#format ,该值为十进制 int而不是十六进制 int

You instead want to map the value in the array directly to a string, then get its decimal value: 相反,您希望将数组中的值直接映射到字符串,然后获取其十进制值:

byte it = 14;
int x = Integer.parseInt(String.valueOf(it), 16);
System.out.println(x); // This will print 20

This should get you the expected result. 这应该为您带来预期的结果。


Note, your problem is slightly confusing because the numbers in your byte array are decimal numbers...already in their hex 'format'. 注意,您的问题有点令人困惑,因为您的字节数组中的数字是十进制数字...已经以其十六进制“格式”显示。 There are a few ways this confusion can be fixed (assuming you have control of the array's type and/or contents): 有几种方法可以解决这种混淆(假设您可以控制数组的类型和/或内容):

  • Use String instead of byte 使用String而不是byte
String it = "14";
int x = Integer.parseInt(it, 16);
System.out.println(x); // This will print 20
  • Store the values as hex and still convert (ie, putting a 0x in front of each number) 将值存储为十六进制并仍进行转换(即,在每个数字前放置一个0x
byte it = 0x14;
int x = Integer.parseInt(String.format("%02X", it), 16);
System.out.println(x); // This will print 20
  • Store the values as hex and don't convert, because there is no need 将值存储为十六进制,并且不进行转换,因为不需要
byte it = 0x14;
System.out.println(it); // This will print 20

If you go with the second bullet, though convoluted, what you have right now should work, but the third option would be best if you are hardcoding in the byte array's values. 如果使用第二个项目符号,尽管有些麻烦,但现在应该可以使用,但是如果您要对字节数组的值进行硬编码,则第三个选项是最好的。

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

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