Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Steven Devijver

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Custom base conversion (to and from)

Handy to store large numbers in a shorter notation. Doesn't work with negative numbers (for negative numbers you need to store an additional byte to indicate a positive or negative value, pass the absolute value).

   1  
   2  	private static final short BASE_40_RADIX = 40;
   3  	private static final short BASE_40_BASE = 48;
   4  	private static final String BASE_40_ZERO = "0";
   5  
   6  	public static long fromBase40(char[] cs) {
   7  		return fromBase(cs, BASE_40_BASE, BASE_40_RADIX);
   8  	}
   9  
  10  	public static char[] toBase40(final long i) {
  11  		return toBase(i, BASE_40_BASE, BASE_40_RADIX, BASE_40_ZERO);
  12  	}
  13  
  14  	private static final short BASE_75_RADIX = 75;
  15  	private static final short BASE_75_BASE = 48;
  16  	private static final String BASE_75_ZERO = "0";
  17  
  18  	public static long fromBase75(char[] cs) {
  19  		return fromBase(cs, BASE_75_BASE, BASE_75_RADIX);
  20  	}
  21  
  22  	public static char[] toBase75(final long i) {
  23  		return toBase(i, BASE_75_BASE, BASE_75_RADIX, BASE_75_ZERO);
  24  	}
  25  
  26  	private static final short BASE_90_RADIX = 90;
  27  	private static final short BASE_90_BASE = 33;
  28  	private static final String BASE_90_ZERO = "!";
  29  
  30  	public static long fromBase90(char[] cs) {
  31  		return fromBase(cs, BASE_90_BASE, BASE_90_RADIX);
  32  	}
  33  
  34  	public static char[] toBase90(final long i) {
  35  		return toBase(i, BASE_90_BASE, BASE_90_RADIX, BASE_90_ZERO);
  36  	}
  37  
  38  	private static char[] toBase(final long i, final short base, final short radix, String nullCharacter) {
  39  		long value = i;
  40  
  41  		if (value == 0) {
  42  			return nullCharacter.toCharArray();
  43  		}
  44  
  45  		String result = "";
  46  		while (value > 0) {
  47  			long mod = value % radix;
  48  			value -= mod;
  49  			if (value > 0) value /= radix;
  50  			result = (char)(mod + base) + result;
  51  		}
  52  
  53  		return result.toCharArray();
  54  	}
  55  
  56  	private static long fromBase(char[] cs, final short base, final short radix) {
  57  		long value = 0;
  58  		for (int i = cs.length - 1; i >= 0; i--) {
  59  			int digit = ((int)cs[i]) - base;
  60  			if (digit < 0 || digit >= radix) {
  61  				throw new IllegalArgumentException("Invalid Base" + radix + " character: " + cs[i]);
  62  			}
  63  			long digitBase = (long)Math.pow(radix, (cs.length - 1) - i);
  64  			value = (digit * digitBase) + value;
  65  		}
  66  
  67  		return value;
  68  	}


And here are some tests:

   1  
   2  	public void testFromBase40() {
   3  		assertEquals(2559999, NumberUtils.fromBase40("WWWW".toCharArray()));
   4  		assertEquals(1117580, NumberUtils.fromBase40("ABCD".toCharArray()));
   5  
   6  		assertEquals(0, NumberUtils.fromBase40("0".toCharArray()));
   7  		assertEquals(1, NumberUtils.fromBase40("1".toCharArray()));
   8  
   9  		assertEquals(40, NumberUtils.fromBase40("10".toCharArray()));
  10  		assertEquals(80, NumberUtils.fromBase40("20".toCharArray()));
  11  
  12  		assertEquals(41, NumberUtils.fromBase40("11".toCharArray()));
  13  		assertEquals(81, NumberUtils.fromBase40("21".toCharArray()));
  14  
  15  		assertEquals(81, NumberUtils.fromBase40("0021".toCharArray()));
  16  
  17  		assertEquals(3061560805L, NumberUtils.fromBase40("MSTSD5".toCharArray()));
  18  	}
  19  
  20  	public void testToBase40() {
  21  		assertEquals("0", new String(NumberUtils.toBase40(0)));
  22  		assertEquals("1", new String(NumberUtils.toBase40(1)));
  23  
  24  		assertEquals("10", new String(NumberUtils.toBase40(40)));
  25  		assertEquals("20", new String(NumberUtils.toBase40(80)));
  26  
  27  		assertEquals("11", new String(NumberUtils.toBase40(41)));
  28  		assertEquals("21", new String(NumberUtils.toBase40(81)));
  29  
  30  		assertEquals("ABCD", new String(NumberUtils.toBase40(1117580)));
  31  		assertEquals("WWWW", new String(NumberUtils.toBase40(2559999)));
  32  
  33  		assertEquals("MSTSD5", new String(NumberUtils.toBase40(3061560805L)));
  34  
  35  	}
  36  
  37  	public void testToBase90() throws Exception {
  38  		assertEquals("#Q&Y.`QY#", new String(NumberUtils.toBase90(10908158098650842L)));
  39  	}
  40  
  41  	public void testFromBase90() throws Exception {
  42  		assertEquals(10908158098650842L, NumberUtils.fromBase90("#Q&Y.`QY#".toCharArray()));
  43  	}

Convert a long to HEX value and the other way around

Handy to convert MD5 or SHA-1 hash values.

   1  
   2  	public static long hexToLong(byte[] bytes) {
   3  
   4  		if (bytes.length > 16) {
   5  			throw new IllegalArgumentException("Byte array too long (max 16 elements)");
   6  		}
   7  		long v = 0;
   8  		for (int i = 0; i < bytes.length; i += 2) {
   9  			byte b1 = (byte) (bytes[i] & 0xFF);
  10  
  11  			b1 -= 48;
  12  			if (b1 > 9) b1 -= 39;
  13  
  14  			if (b1 < 0 || b1 > 15) {
  15  				throw new IllegalArgumentException("Illegal hex value: " + bytes[i]);
  16  			}
  17  
  18  			b1 <<=4;
  19  
  20  			byte b2 = (byte) (bytes[i + 1] & 0xFF);
  21  			b2 -= 48;
  22  			if (b2 > 9) b2 -= 39;
  23  
  24  			if (b2 < 0 || b2 > 15) {
  25  				throw new IllegalArgumentException("Illegal hex value: " + bytes[i + 1]);
  26  			}
  27  
  28  			v |= (((b1 & 0xF0) | (b2 & 0x0F))) & 0x00000000000000FFL ;
  29  
  30  			if (i + 2 < bytes.length) v <<= 8;
  31  		}
  32  
  33  		return v;
  34  	}
  35  
  36  	public static byte[] longToHex(final long l) {
  37  		long v = l & 0xFFFFFFFFFFFFFFFFL;
  38  
  39  		byte[] result = new byte[16];
  40  		Arrays.fill(result, 0, result.length, (byte)0);
  41  
  42  		for (int i = 0; i < result.length; i += 2) {
  43  			byte b = (byte) ((v & 0xFF00000000000000L) >> 56);
  44  
  45  			byte b2 = (byte) (b & 0x0F);
  46  			byte b1 = (byte) ((b >> 4) & 0x0F);
  47  
  48  			if (b1 > 9) b1 += 39;
  49  			b1 += 48;
  50  
  51  			if (b2 > 9) b2 += 39;
  52  			b2 += 48;
  53  
  54  			result[i] = (byte) (b1 & 0xFF);
  55  			result[i + 1] = (byte) (b2 & 0xFF);
  56  
  57  			v <<= 8;
  58  		}
  59  
  60  		return result;
  61  	}
  62  


And tests:

   1  
   2  	public void testHexToLong() throws Exception {
   3  		assertEquals(-7057002501900618110L, NumberUtils.hexToLong("9e107d9d372bb682".getBytes()));
   4  		assertEquals(-10908158098650842L, NumberUtils.hexToLong("ffd93f1687604926".getBytes()));
   5  	}
   6  
   7  	public void testLongToHex() throws Exception {
   8  		assertEquals("9e107d9d372bb682", new String(NumberUtils.longToHex(-7057002501900618110L)));
   9  		assertEquals("ffd93f1687604926", new String(NumberUtils.longToHex(-10908158098650842L)));
  10  	}
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS