55

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!

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • 2
    Try to add a "0x" prefix to the string before calling valueOf – sinelaw Mar 01 '11 at 11:27
  • 3
    Umm ... did you look in the javadocs? Save yourself time and always look in the javadocs first. – Stephen C Mar 01 '11 at 11:27
  • 2
    Also, valueOf should accept a second parameter that specifies the base. Try passing 16 (and the string without the prefix) – sinelaw Mar 01 '11 at 11:28
  • 3
    @sinelaw - I think you are confusing `Long.valueOf(String)` with `Long.decode(String)`. The Javadoc for `valueOf(String)` says *"Parses the string argument as a signed decimal long."* – Stephen C Mar 01 '11 at 11:32
  • Stephen, right, it's decode that accepts the prefix. But valueOf accepts the base parameter (see link above) – sinelaw Mar 01 '11 at 11:39

4 Answers4

100

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

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
47

Use parseLong:

Long.parseLong(s, 16)
Erik
  • 88,732
  • 13
  • 198
  • 189
15
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.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Bobby
  • 151
  • 1
  • 2
10

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;
    }
}
quezacoatl
  • 301
  • 2
  • 5
  • 1
    This 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. – Coronel Kittycannon Dec 20 '19 at 19:21