Created
October 3, 2018 05:48
-
-
Save arbing/a33157c079bac3bdef4e297a98db59ee to your computer and use it in GitHub Desktop.
BaseConverter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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