HexString { public static long toLong(String values) throws NumberFormatException { BigInteger bi = new BigInteger(values.replaceAll(":", ""),16); if (bi.bitLength() > 64) throw new NumberFormatException("Input string too big to fit in long: " + values); return bi.longValue(); }  static String toHexString(byte[] bytes); static String toHexString(long val, int padTo); static String toHexString(long val); static byte[] fromHexString(String values); static long toLong(String values);  }
@Test public void testToLongMSB() { String dpidStr = "ca:7c:5e:d1:64:7a:95:9b"; long valid = -3856102927509056101L; long testLong = HexString.toLong(dpidStr); TestCase.assertEquals(valid, testLong); }
@Test public void testToLongError() { String dpidStr = "09:08:07:06:05:04:03:02:01"; try { HexString.toLong(dpidStr); fail("HexString.toLong() should have thrown a NumberFormatException"); } catch (NumberFormatException expected) { } }
@Test public void testToLong() { String dpidStr = "3e:1f:01:fc:72:8c:63:31"; long valid = 0x3e1f01fc728c6331L; long testLong = HexString.toLong(dpidStr); TestCase.assertEquals(valid, testLong); }