十六进制到整数(4个字节) [英] Hex to integer (4 bytes)
问题描述
我正在使用ZigBee,我从终端设备的传感器接收数据,并且希望将十六进制转换为整数.数据来自4bytes,该信息与电池有关,我得到的一个例子是:"00 00 00 e1"(但无空格),我需要将此传递给电压,但首先我想我需要将其传递给int.
I'm working with ZigBee, I receive data from a sensor of a end device, and I want to convert from hex to int. The data are from 4bytes, this information is regarding the battery, an example of what I get would be this: "00 00 00 e1" (but without blanks) and I need to pass this on to voltages, but first I think I need to pass it to int.
任何人都可以帮助我吗?我正在为Android开发应用程序.
Anyone can help me please? I'm doing my application for Android.
谢谢.
推荐答案
int n = (int)Long.parseLong(st.replaceAll("\\s+",""), 16);
(其中st是您的字符串)可以执行此操作.两件事:
(where st is your string) does it. Two things:
replaceAll
会删除任何空白.
我通过 Long
解析器绕过 NumberFormatExceptions
否定项
I go via the Long
parser to circumvent NumberFormatExceptions
for negatives
这篇关于十六进制到整数(4个字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!