How to convert a hexadecimal string to long in java?
Solution 1
Long.decode(str)
accepts a variety of formats:
Accepts decimal, hexadecimal, and octal numbers given by the following grammar:
DecodableString:
- Signopt DecimalNumeral
- Signopt 0x HexDigits
- Signopt 0X HexDigits
- Signopt # HexDigits
- Signopt 0 OctalDigits
Sign:
- -
But in your case that won't help, your String is beyond the scope of what long can hold. You need a BigInteger
:
String s = "4d0d08ada45f9dde1e99cad9";
BigInteger bi = new BigInteger(s, 16);
System.out.println(bi);
Output:
23846102773961507302322850521
For Comparison, here's Long.MAX_VALUE
:
9223372036854775807
Solution 2
Use parseLong:
Long.parseLong(s, 16)
Solution 3
new BigInteger(string, 16).longValue()
For any value of someLong:
new BigInteger(Long.toHexString(someLong), 16).longValue() == someLong
In other words, this will return the long
you sent into Long.toHexString()
for any long
value, including negative numbers. It will also accept strings that are bigger than a long
and silently return the lower 64 bits of the string as a long
. You can just check the string length <= 16 (after trimming whitespace) if you need to be sure the input fits in a long
.
Solution 4
Long.parseLong(s, 16)
will only work up to "7fffffffffffffff"
. Use BigInteger
instead:
public static boolean isHex(String hex) {
try {
new BigInteger(hex, 16);
return true;
} catch (NumberFormatException e) {
return false;
}
}
Admin
Updated on July 19, 2022Comments
-
Admin almost 2 years
I want to convert a hex string to long in java.
I have tried with general conversion.
String s = "4d0d08ada45f9dde1e99cad9"; long l = Long.valueOf(s).longValue(); System.out.println(l); String ls = Long.toString(l);
But I am getting this error message:
java.lang.NumberFormatException: For input string: "4d0d08ada45f9dde1e99cad9"
Is there any way to convert String to long in java? Or am i trying which is not really possible!!
Thanks!
-
Sean Patrick Floyd about 13 yearsTrue in general, but fails in this case (the number is much too large for a Long)
-
Dead Programmer about 13 yearsstill it throws NumberFormatException,it's too long.
-
Erik about 13 years@Suresh: Yes, your sample string cannot be converted to a long, I was just answering "How to convert a hexadecimal string to long". You may want to look at BigInteger as Sean Patrick Floyd mentioned.
-
LCoelho over 9 yearsThe goal is to convert to a Long, right? Then this is the clean and easy way to do it!
-
J E Carter II about 9 yearsNot sure about System.out.println method signature, but using bi in a String assignment I needed to use bi.toString()
-
Sean Patrick Floyd over 8 years@JECarterII println on an Object calls that object's toString() method
-
user207421 almost 8 years@SeanPatrickFloyd That's the OP's problem, not this answer's.
-
JonSlowCN over 6 years@SeanPatrickFloyd If the string cannot be converted to a Long, it should fail and throw an exception. It's the developer's job to handle that exception.
-
ashdnik almost 6 yearsCrisp Clear Thanks!
-
PLG over 4 yearsBy printing the stack trace, it seems that
Long.decode()
just callsLong.parseLong()
. -
Sean Patrick Floyd over 4 years@PLG it's a facade around
Long.valueOf
, which in turn usesLong.parseLong
. The specific purpose ofLong.decode
is to detect whether the input string is decimal, octal, or hexadecimal, and then callLong.valueOf
with the detected base. -
Coronel Kittycannon over 4 yearsThis is what no-one is saying. Technically, you should be able to parse a unsigned long with a string with lenght 16 and only F's (FFFFFFFFFFFFFFFF), but Long.parseLong() only works up to 7fffffffffffffff, like you said.