利用系统API
// 十进制-->其他进制
System.out.println(Integer.toBinaryString(16));//2进制,10000
System.out.println(Integer.toOctalString(16));//8进制,20
System.out.println(Integer.toHexString(16));//16进制,10
System.out.println(Integer.toString(16, 2));//可转化为任意进制,10000
System.out.println("*******************************************");
// 某进制的字符串-->十进制数
System.out.println(Integer.parseInt("-06"));//-6,将十进制字符串转换为十进制数
System.out.println(Integer.parseInt("-10", 2));//-2,将二进制字符串转换为十进制数
System.out.println(Integer.parseInt("-10", 16));//-16,将16进制字符串转换为十进制数 10进制转16进制
/**
* 将int类型10进制数转为16进制
*/
public static String toHex(int num) {
if (num <= 9 && num >= 0) return num + "";
// 查表法
char[] chs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; //长度为16
char[] arr = new char[8];// 定义一个数组,存储16进制各个位上的字符,int为32位,2^32=16^8,所以只需8位即可
int pos = arr.length;
while (num != 0) {
int temp = num & 15; //取2进制数的低4位,对应转化为16进制数的低1位,此16进制此位的值刚好对应数组chs里相应下标的元素的值
arr[--pos] = chs[temp]; //将上述字符赋值给数组arr的最后一个元素
num = num >>> 4; //无符号右移,取完之后右移4位,判断是否为0,不为0继续取直到为0
}
StringBuilder str = new StringBuilder();
for (int x = pos; x < arr.length; x++) { // 高位的0不打印
str.append(arr[x]);
}
str.append("\n");
return str.toString();
} 10进制转任意进制
/**
* 将int类型10进制数转为其他进制,方法一
* @param num 要转换的十进制数
* @param base 要转换成的进制数,取值范围为[2,16]
* @return 转换结果,为简化算法,以字符串格式输出
*/
public static String baseTran_1(int num, int base) {
if (base > 16 || base < 2) throw new RuntimeException("不支持的进制数");
if (num == 0 || base == 10) return num + "";
char[] chs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char[] arr = new char[32];//存储int类型数据,16进制需8位(16^8),2进制需32位(2^32)
int pos = arr.length;//即32
while (num != 0) { //假设num=45,offset=16
int temp = num & (base - 1);//即 temp =0010-1101 & 1111 = 0000-1101 即 13
arr[--pos] = chs[temp];//即 arr[31] = chs[13] = 'D'
num = num >>> (int) Math.sqrt(base);//即 num = 0010
}
StringBuilder str = new StringBuilder("");
for (int x = pos; x < arr.length; x++) {
str.append(arr[x]);
}
return str.toString();
}
/**
* 将int类型10进制数转为其他进制,方法二
* @param num 要转换的十进制数
* @param base 要转换成的进制数,取值范围为[2,16]
* @return 转换结果,为简化算法,以字符串格式输出
*/
public static String baseTran_2(int num, int base) {
if (base > 16 || base < 2) throw new RuntimeException("不支持的进制数");
if (num == 0 || base == 10) return num + "";
String digths = "0123456789ABCDEF";
Stack<Character> s = new Stack<Character>();
while (num != 0) {
s.push(digths.charAt(num % base));
num /= base;
}
StringBuffer str = new StringBuffer("");
while (!s.isEmpty()) {
str.append(s.pop());
}
return str.toString();
} 任意进制转化为10进制
以下为 Integer.parseInt(String paramString, int paramInt)的源码
public static int parseInt(String paramString, int paramInt) throws NumberFormatException {
if (paramString == null) throw new NumberFormatException("null");
if (paramInt < 2) throw new NumberFormatException("radix " + paramInt + " less than Character.MIN_RADIX");
if (paramInt > 36) throw new NumberFormatException("radix " + paramInt + " greater than Character.MAX_RADIX");
int i = 0;
int j = 0;
int k = 0;
int m = paramString.length();
int n = -2147483647;
if (m > 0) {
int i3 = paramString.charAt(0);
if (i3 < 48) {
if (i3 == 45) {
j = 1;
n = Integer.MIN_VALUE;
} else if (i3 != 43) throw new RuntimeException("失败");//其实是:throw NumberFormatException.forInputString(paramString);
if (m == 1) throw new RuntimeException("失败");
k++;
}
int i1 = n / paramInt;
while (k < m) {
int i2 = Character.digit(paramString.charAt(k++), paramInt);
if (i2 < 0) throw new RuntimeException("失败");
if (i < i1) throw new RuntimeException("失败");
i *= paramInt;
if (i < n + i2) throw new RuntimeException("失败");
i -= i2;
}
} else throw new RuntimeException("失败");
return j != 0 ? i : -i;
} byte[]和16进制字符串相互装换
public class Test {
public static void main(String[] args) throws Exception {
System.out.println(bytesToHexString(new byte[] { '\n', '1', 'a' }));//0a-31-61
System.out.println(Arrays.toString(hexStringToBytes("0a3161")));//[10, 49, 97]
}
/**
* 将byte[]转换成16进制字符串形式
*/
public static String bytesToHexString(byte[] src) {
if (src == null || src.length <= 0) return null;
StringBuilder stringBuilder = new StringBuilder("");
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;//将byte转换成int,这一步是核心代码
String hv = Integer.toHexString(v);//把int转换成16进制的字符串
if (hv.length() < 2) stringBuilder.append(0);
stringBuilder.append(hv);
if (i < src.length - 1) stringBuilder.append("-");
}
return stringBuilder.toString();
}
/**
* 将16进制形式字符串转换成byte[]
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) return null ;
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c); //将2-16进制字符串中的char转换成 byte
}
}