十六进制,八进制,二进制转换为十进制
public class RadixConvertUtil {
public static final String BIN_REGEX = "^[\\+,\\-]?[0|1]+[\\.]?[0|1]*$";
public static final String OCT_REGEX = "^[\\+,\\-]?[0-7]+[\\.]?[0-7]*$";
public static final String DEC_REGEX = "^[\\+,\\-]?[0-9]+[\\.]?[0-9]*$";
public static final String HEX_REGEX = "^[\\+,\\-]?[A-Fa-f0-9]+[\\.]?[A-Fa-f0-9]*$";
private final static char CHAR_ZERO = '0';
private final static char CHAR_ADD = '+';
private final static char CHAR_SUB = '-';
private final static String STR_DOT = ".";
/**
* 二进制字符串转换为十进制字符串
* 例如:
* octToDec("11") return "3"
* octToDec("-11") return "-3"
* octToDec("+11") return "3"
* octToDec("11.1") return "3.5"
* octToDec("-11.1") return "-3.5"
* octToDec("+11.1") return "3.5"
* @param data 二进制字符串
* @return 十进制字符串
*/
public static String binToDec(String data) {
return checkAndToDec(data, BIN_REGEX, RadixEnum.BIN);
}
/**
* 八进制字符串转换为十进制字符串
* 例如:
* octToDec("11") return "9"
* octToDec("-11") return "-9"
* octToDec("+11") return "9"
* octToDec("11.1") return "9.125"
* octToDec("-11.1") return "-9.125"
* octToDec("+11.1") return "9.125"
* @param data 八进制字符串
* @return 十进制字符串
*/
public static String octToDec(String data) {
return checkAndToDec(data, OCT_REGEX, RadixEnum.OCT);
}
/**
* 十六进制字符串转换为十进制字符串
* 例如:
* hexToDec("11") return "17"
* hexToDec("-11") return "-17"
* hexToDec("+11") return "17"
* hexToDec("11.1") return "17.0625"
* hexToDec("-11.1") return "-17.0625"
* hexToDec("+11.1") return "17.0625"
* @param data 十六进制字符串
* @return 十进制字符串
*/
public static String hexToDec(String data) {
return checkAndToDec(data, HEX_REGEX, RadixEnum.HEX);
}
private static String checkAndToDec(String data, String regex, RadixEnum radixEnum) {
if (isNotLegal(data, regex)) {
throw new RuntimeException("非法字符");
}
int i = 0;
int len = data.length();
if (len > 0) {
char firstChar = data.charAt(i);
boolean negative = false;
if (firstChar < CHAR_ZERO) {
if (firstChar == CHAR_SUB) {
negative = true;
} else if (firstChar != CHAR_ADD) {
throw new NumberFormatException("非法字符");
}
i++;
}
String result = toDec(data.substring(i), radixEnum.getCode());
return negative ? "-" + result : result;
} else {
throw new NumberFormatException("长度为0");
}
}
/**
* 字符串是否是不合法的
* @param data radix进制字符串
* @param regex 正则表达式
* @return ture 是 / false 否
*/
private static boolean isNotLegal(String data, String regex) {
if (data == null) {
return true;
}
Pattern pattern = Pattern.compile(regex);
return !pattern.matcher(data).matches();
}
/**
* radix进制字符串转十进制字符串
* 假设某个进制字符串为abc.def
* 如果为16进制 则转换为十进制为 a * 16^2 + b * 16^1 + c ^ 16^0 + d * 16^-1 + e * 16^-2 + f * 16^-3
* 如果为8进制 则转换为十进制为 a * 8^2 + b * 8^1 + c ^ 8^0 + d * 8^-1 + e * 8^-2 + f * 8^-3
* 如果为2进制 则转换为十进制为 a * 2^2 + b * 2^1 + c ^ 2^0 + d * 2^-1 + e * 2^-2 + f * 2^-3
* @param data radix进制字符串
* @param radix 进制
* @return 十进制字符串
*/
private static String toDec(String data, int radix) {
// 是否包含小数,包含则分成两部分转换
if (data.contains(STR_DOT)) {
String[] split = data.split("\\.");
// 整数部分
Long l = integerToDec(split[0], radix);
// 小数部分
Double d = decimalToDec(split[1], radix);
return String.valueOf(l + d);
} else {
// 整数部分
return String.valueOf(integerToDec(data, radix));
}
}
private static Long integerToDec(String data, int radix) {
char[] chars = data.toCharArray();
int length = chars.length;
int i = 0;
long result = 0;
while (i < length) {
int digit = Character.digit(chars[i++], radix);
result *= radix;
result += digit;
}
return result;
}
private static Double decimalToDec(String data, int radix) {
char[] chars = data.toCharArray();
int length = chars.length;
int i = length - 1;
double result = 0.0;
while (i >= 0) {
int digit = Character.digit(chars[i--], radix);
result += digit;
result *= 1.0/radix;
}
return result;
}
}
枚举
public enum RadixEnum {
/**
* 进制
*/
BIN(2),
OCT(8),
DEC(10),
HEX(16);
private final Integer code;
RadixEnum(Integer code) {
this.code = code;
}
public Integer getCode() {
return code;
}
}