int n = (int)Long.parseLong(st.replaceAll("\\s+",""), 16);
(where st is your string) does it. Two things:
The replaceAll
strips out any white space.
I go via the Long
parser to circumvent NumberFormatExceptions
for negatives
Frage
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.
Anyone can help me please? I'm doing my application for Android.
Thank you in advance.
Lösung
int n = (int)Long.parseLong(st.replaceAll("\\s+",""), 16);
(where st is your string) does it. Two things:
The replaceAll
strips out any white space.
I go via the Long
parser to circumvent NumberFormatExceptions
for negatives
Andere Tipps
Use Integer.parseInt()
for this:
Parses the string argument as a signed integer in the radix specified by the second argument
Example:
Integer.parseInt("-FF", 16) // pass 16 to indicate an hexadecimal value
=> -255
Integer.parseInt("ff0000", 16)