// // Hex class // part of the set of documents known as Java no sugar. // Copyright (c) 1996 Sunil Gupta, sunil@magnetic.demon.co.uk // placed into the public domain by the author // public class Hex extends Object { private static final String hexCodes = "0123456789abcdef"; private static final String hexPrefix = "0x"; public static String toHex(long value) { long remainder; byte digit; String answer = ""; remainder = value; while (remainder > 0) { digit = (byte) (remainder & 0xFl); answer = hexCodes.charAt(digit) + answer; remainder >>= 4; } return (hexPrefix + answer); } public static String toHex(int value) { return toHex((long) value); } public static String toHex(short value) { return toHex((long) value); } public static String toHex(byte value) { return toHex((long) value); } //############################################################################# // testing //############################################################################# public static void main( String argv[]) { String result; result = toHex(0x1234567812345678l); if ( result.equals("0x1234567812345678") ) System.out.println("Hex conversion worked just fine"); else System.out.println( "Hex conversion is b*******d" + "\n\tExpected: 0x1234567812345678" + "\n\t Got: " + result); } }
.