Skip to content

Instantly share code, notes, and snippets.

@arbing
Created October 3, 2018 05:48
Show Gist options
  • Save arbing/a33157c079bac3bdef4e297a98db59ee to your computer and use it in GitHub Desktop.
Save arbing/a33157c079bac3bdef4e297a98db59ee to your computer and use it in GitHub Desktop.
BaseConverter
package com.github.arbing.common.utils;
import org.apache.commons.lang3.StringUtils;
public class BaseConverter {
private final static String baseMap = "96735ecfd824b1a0";
/**
* 从10进制转换
*
* @param number 要转换的数字
* @return 转换后
*/
public static String convertFromDecimal(long number) {
int baseTo = baseMap.length();
StringBuffer sum = new StringBuffer();
while (number != 0) {
long remainder = number % baseTo;
sum.insert(0, baseMap.charAt((int) remainder));
number /= baseTo;
}
return sum.toString();
}
/**
* 转换成10进制
*
* @param str 要转换的数字
* @return 转换后
*/
public static long convertToDecimal(String str) {
int baseFrom = baseMap.length();
long sum = 0;
str = StringUtils.reverse(str);
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
long val = baseMap.indexOf(ch);
sum += val * Math.pow(baseFrom, i);
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment