tags:

views:

1202

answers:

2

My component is handed a long value that I later use as a key into a cache. The key itself is a string representation of the long value as if it were unsigned 64-bit value. That is, when my component is handed -2944827264075010823L, I need to convert that into the string key "15501916809634540793".

I have a solution, but it seems brute force and it makes me a bit queasy. Essentially, I convert the long into a hexadecimal string representation (so -2944827264075010823L becomes "d721df34a7ec6cf9") and convert the hexadecimal string into a BigInteger:

String longValueAsHexString = convertLongToHexString(longValue);
BigInteger bi = new BigInteger(longValueAsHexString, 16);
String longValueString = bi.toString();

I then use longValueString as the key into the cache.

I cannot use Long.toString(longValue,16), because it returns the hex string for the absolute value, prefixed by a "-".

So my convertLongToHexString looks like this:

long mask = 0x00000000ffffffffL;
long bottomHalf = number & mask;
long upperHalf = (number >> 32) & mask;
String bottomHalfString = Long.toString(bottomHalf, 16);
if (bottomHalfString.length() != 8) {
    String zeroes = "0000000000000000";
    bottomHalfString = zeroes.substring(16-bottomHalfString.length()) + bottomHalfString;
}
return Long.toString(upperHalf,16)+bottomHalfString;

There must be a more elegant way of doing this. Any suggestions?

+3  A: 

Here's my implementation. I've refactored it to have a function taking a long and returning a string. :-)

import java.math.BigInteger;

class UInt64Test {
    public static void main(String[] args) {
        for (String arg : args)
            System.out.println(toUnsignedString(Long.parseLong(arg)));
    }

    private static final BigInteger B64 = BigInteger.ZERO.setBit(64);
    public static String toUnsignedString(long num) {
        if (num >= 0)
            return String.valueOf(num);
        return BigInteger.valueOf(num).add(B64).toString();
    }
}
Chris Jester-Young
+1  A: 

I think Chris's answer is better, but here's another just for fun.

public static String longUnsignedString(long l) {
  byte[] bytes = new byte[9];

  for (int i = 1; i < 9; i++) {
     bytes[i] = (byte) ((l >> ((8 - i) * 8)) & 255);
  }

  return (new BigInteger(bytes)).toString();
}
Joshua Swink