话不多说上代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 进制转换
*/
public class ConvHandler {
private static List<Integer> binaryList = new ArrayList<>();
private static Map<String, String> _16Mapping = new HashMap<>();
public static final Integer _2_BINARY = 2;
public static final Integer _8_BINARY = 8;
public static final Integer _16_BINARY = 16;
static {
binaryList.add(_2_BINARY);
binaryList.add(_8_BINARY);
binaryList.add(_16_BINARY);
_16Mapping.put("A", "10");
_16Mapping.put("B", "11");
_16Mapping.put("C", "12");
_16Mapping.put("D", "13");
_16Mapping.put("E", "14");
_16Mapping.put("F", "15");
}
/**
* 二进制、八进制和十六进制 转 十进制
* @param binaryData 要转化的进制数字符
* @param convBinary binaryData是几进制
* @return
*/
public static Double binaryConvTo10(String binaryData, Integer convBinary){
if(convBinary == null || StringUtils.isBlank(binaryData)){
return null;
}
if (!binaryList.contains(convBinary)) {
return null;
}
for (String item :binaryData.split(".")) {
if(!StringUtils.isNumeric(item)){
return null;
}
}
return conv10(binaryData, convBinary);
}
/**
* 十进制 转 二进制、八进制和十六进制
* @param binaryData 转换的10进制数
* @param convBinary 要转换成的进制,只能是二进制、八进制和十六进制
* @return
*/
public static Double _10ToOtherBinary(Double binaryData, Integer convBinary){
if(check10Binary(binaryData, convBinary)){
return null;
}
String value = String.valueOf(binaryData);
String zhengshu = value;
String xiaoshu = "";
if(value.contains(".")){
zhengshu = value.substring(0, value.lastIndexOf("."));
xiaoshu = value.substring(value.lastIndexOf(".") + 1);
}
Integer zhengshuInt = Integer.parseInt(zhengshu);
StringBuilder sb = new StringBuilder();
while(true){
int b = Math.floorMod(zhengshuInt, convBinary);
sb.append(b);
if((zhengshuInt = zhengshuInt / convBinary) == 0){
break;
}
}
String str = sb.toString();
char[] chars = new char[str.length()];
for(int i = str.length() - 1,j = 0; i >= 0 ; i--, j++){
chars[j] = str.charAt(i);
}
zhengshu = String.valueOf(chars);
double xiaoshuDouble = Double.valueOf("0." + xiaoshu);
sb = new StringBuilder();
while(xiaoshuDouble > 0){
xiaoshuDouble *= convBinary;
sb.append((int)xiaoshuDouble);
String breakIf = String.valueOf(xiaoshuDouble);
breakIf = breakIf.substring(breakIf.indexOf(".") + 1);
xiaoshuDouble = Double.valueOf("0." + breakIf);
int i = Integer.parseInt(breakIf);
if(i == 0){
break;
}
}
xiaoshu = sb.toString();
return Double.valueOf(zhengshu + "." + xiaoshu);
}
private static Double conv10(String binaryData, Integer convBinary) {
if(check(binaryData, convBinary)){
Double result = 0.0;
String zhengshu = binaryData;
String xiaoshu = "";
if(binaryData.contains(".")){
zhengshu = binaryData.substring(0, binaryData.lastIndexOf("."));
xiaoshu = binaryData.substring(binaryData.lastIndexOf(".") + 1);
}
double basic = convBinary;
for(int i = zhengshu.length() - 1, j = 0; i >= 0 ; i--, j++){
Double item = Double.valueOf(_16CharProcessing(String.valueOf(zhengshu.charAt(i)), convBinary, i, zhengshu.length()));
Double in = Double.parseDouble(String.valueOf(j));
double pow = Math.pow(basic, in);
result += (item * pow);
}
for(int i = 0, j = -1; i < xiaoshu.length(); i++, j--){
Double item = Double.valueOf(_16CharProcessing(String.valueOf(xiaoshu.charAt(i)), convBinary, i, xiaoshu.length()));
Double in = Double.parseDouble(String.valueOf(j));
double pow = Math.pow(basic, in);
result += item * pow;
}
return result;
}
return null;
}
private static boolean check10Binary(Double binaryData, Integer convBinary){
if(binaryData == null || !binaryList.contains(convBinary)){
return true;
}
return false;
}
private static boolean check(String binaryData, Integer convBinary){
if(!binaryList.contains(convBinary) || StringUtils.isBlank(binaryData)){
return false;
}
Integer isDouble = 0;
if(convBinary.equals(_8_BINARY) || convBinary.equals(_2_BINARY)){
for(int i = 0; i < binaryData.length(); i++){
if(isDouble == 2){
return false;
}
String item = String.valueOf(binaryData.charAt(i));
if(StringUtils.equals(".", item)){
isDouble++;
continue;
}
if(!StringUtils.isNumeric(item)){
return false;
}
}
} else {
for(int i = 0; i < binaryData.length(); i++){
if(isDouble == 2){
return false;
}
String item = String.valueOf(binaryData.charAt(i));
if(StringUtils.equals(".", item)){
isDouble++;
continue;
}
String value = String.valueOf(binaryData.charAt(i)).toUpperCase();
if(!_16Mapping.containsKey(value)){
if(!StringUtils.isNumeric(value)){
return false;
}
if(Integer.parseInt(value) < 0 || Integer.parseInt(value) > 9 ){
return false;
}
}
}
}
return true;
}
private static String _16CharProcessing(String item, Integer convBinary, int index, int length){
if(convBinary == _16_BINARY){
item = item.toUpperCase();
if(_16Mapping.containsKey(item)){
item = _16Mapping.get(item);
}
}
return item;
}
}
测试类
public class Main {
public static void main(String[] args) {
System.out.println(ConvHandler.binaryConvTo10("1010.1101", 2));
System.out.println(ConvHandler.binaryConvTo10("423.5176", 8));
System.out.println(ConvHandler.binaryConvTo10("9FA8C", 16));
// System.out.println(0x9FA8C);
System.out.println("=======================================================");
System.out.println(ConvHandler._10ToOtherBinary(10.0,2));
System.out.println(ConvHandler._10ToOtherBinary(0.930908203125,8));
System.out.println(ConvHandler._10ToOtherBinary(0.6875,2));
}
}
StringUtils工具类
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>