Custom base conversion (to and from)
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 }