Java - parse and unsigned hex string into a signed

2020-08-09 08:38发布

问题:

I have a bunch of hex strings, one of them, for example is:

  d1bc4f7154ac9edb

which is the hex value of "-3333702275990511909". This is the same hex you get if you do Long.toHexString("d1bc4f7154ac9edb");

For now, let's just assume I only have access to the hex string values and that is it. Doing this:

  Long.parseLong(hexstring, 16);

Doesn't work because it converts it to a different value that is too large for a Long. Is there away to convert these unsigned hex values into signed longs?

Thanks!

回答1:

You can use BigInteger to parse it and get back a long:

long value = new BigInteger("d1bc4f7154ac9edb", 16).longValue();
System.out.println(value); // this outputs -3333702275990511909


回答2:

You may split it in half and read 32 bits at a time. Then use shift-left by 32 and a logical or to get it back into a single long.



回答3:

The method below has the benefit of not creating another BigInteger object every time you need to do this.

public class Test {
  /**
   * Returns a {@code long} containing the least-significant 64 bits of the unsigned hexadecimal input.
   * 
   * @param  valueInUnsignedHex     a {@link String} containing the value in unsigned hexadecimal notation
   * @return                        a {@code long} containing the least-significant 64 bits of the value
   * @throws NumberFormatException  if the input {@link String} is empty or contains any nonhexadecimal characters
   */
  public static final long fromUnsignedHex(final String valueInUnsignedHex) {
    long value = 0;

    final int hexLength = valueInUnsignedHex.length();
    if (hexLength == 0) throw new NumberFormatException("For input string: \"\"");
    for (int i = Math.max(0, hexLength - 16); i < hexLength; i++) {
      final char ch = valueInUnsignedHex.charAt(i);

      if      (ch >= '0' && ch <= '9') value = (value << 4) | (ch - '0'         );
      else if (ch >= 'A' && ch <= 'F') value = (value << 4) | (ch - ('A' - 0xaL));
      else if (ch >= 'a' && ch <= 'f') value = (value << 4) | (ch - ('a' - 0xaL));
      else                             throw new NumberFormatException("For input string: \"" + valueInUnsignedHex + "\"");
    }

    return value;
  }

  public static void main(String[] args) {
    System.out.println(fromUnsignedHex("d1bc4f7154ac9edb"));
  }
}

This produces

-3333702275990511909


回答4:

The prior answers are overly complex or out of date.

Long.parseUnsignedLong(hexstring, 16)