Java Utililty Methods Base Convert

List of utility methods to do Base Convert

Description

The list of methods to do Base Convert are organized into topic(s).

Method

longfromBase(String num, String baseChars)
Decode number from any base.
int base = baseChars.length();
long x = 0;
if (num != null)
    for (int i = 0; i < num.length(); i++) {
        char c = num.charAt(i);
        int p = baseChars.indexOf(c);
        if (p < 0)
            throw new RuntimeException("fromBase() Invalid character '" + c + "' at position " + i);
...
StringfromBase10(final long base10)
from Base
if (base10 == 0) {
    return "0";
long temp = base10;
final StringBuilder sb = new StringBuilder();
while (temp > 0) {
    temp = fromBase10(temp, sb);
return sb.reverse().toString();
byte[]fromBase16(String data)
from Base
boolean extraByte = data.length() % 2 == 1;
byte[] result = new byte[data.length() / 2 + (extraByte ? 1 : 0)];
for (int i = 0; i < result.length; i++) {
    int value = Integer.parseInt(data.substring(2 * i, Math.min(2 * i + 2, data.length())), 16);
    result[i] = (byte) (value & 0xff);
return result;
byte[]fromBase16toByteArray(String bytes)
This method return a byte[] from a String.
int[] values = fromBase16toIntArray(bytes);
byte[] returns = new byte[values.length];
for (int i = 0; i < values.length; i++) {
    returns[i] = (byte) (values[i] & 0xFF);
return returns;
int[]fromBase16toIntArray(String bytes)
from Baseto Int Array
final String PATTERN = "\\s*((0x[0-9a-f]{2}|[0-9a-f]{2})\\s*)+";
bytes = bytes.toLowerCase();
if (!bytes.matches(PATTERN)) {
    throw new IllegalArgumentException("Unable to parse " + bytes + " doesn't match regex " + PATTERN);
String[] singleBytes = bytes.split("\\s+");
String item;
int[] values = new int[singleBytes.length];
...
intfromBase26(String number)
Convert a base-26 representation into a number, considering it to represent a value in base-26 using the characters a-z or A-Z - the string is treated case-insensitively.
if (!number.matches("^[a-zA-Z]*$"))
    throw new NumberFormatException("Non-alphabetic characters in string.");
int s = 0;
if (number != null && number.length() > 0) {
    number = number.toLowerCase();
    s = (number.charAt(0) - 'a');
    for (int i = 1; i < number.length(); i++) {
        s *= 26;
...
intfromBase36(String base36)

Takes a Base 36 String and returns the Base 10 Integer equivalent

return Integer.parseInt(base36, 36);
intfromBase36(String base36Number)
from Base
return fromOtherBaseToDecimal(36, base36Number);
byte[]fromBase58(String input)
from Base
if (input.length() == 0) {
    return new byte[0];
byte[] input58 = new byte[input.length()];
for (int i = 0; i < input.length(); ++i) {
    char c = input.charAt(i);
    int digit58 = -1;
    if (c >= 0 && c < 128) {
...
intfromBase62(String base62Number)
from Base
return fromOtherBaseToDecimal(62, base62Number);