进制转化工具类
import cn.hutool.core.convert.Convert;
public class ParseSystemUtil {
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase()+" ");
}
return sb.toString();
}
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) {
return null;
}
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
public static int binary2Decimal(String number) {
return scale2Decimal(number, 2);
}
public static int scale2Decimal(String number, int scale) {
checkNumber(number);
if (2 > scale || scale > 32) {
throw new IllegalArgumentException("scale is not in range");
}
int total = 0;
String[] ch = number.split("");
int chLength = ch.length;
for (int i = 0; i < chLength; i++) {
total += Integer.valueOf(ch[i]) * Math.pow(scale, chLength - 1 - i);
}
return total;
}
public static String decimal2Binary(int number) {
return decimal2Scale(number, 2);
}
public static String decimal2Scale(int number, int scale) {
if (2 > scale || scale > 32) {
throw new IllegalArgumentException("scale is not in range");
}
String result = "";
while (0 != number) {
result = number % scale + result;
number = number / scale;
}
return result;
}
public static String two2sixteen(String number) {
int i = Integer.parseInt(number, 2);
return Integer.toHexString(i);
}
public static void checkNumber(String number) {
String regexp = "^\\d+$";
if (null == number || !number.matches(regexp)) {
throw new IllegalArgumentException("input is not a number");
}
}
public static String hexStr2String(String hexString) {
if (hexString == null || hexString.length() % 2 != 0) {
return null;
}
String bString = "", tmp;
for (int i = 0; i < hexString.length(); i++) {
tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
bString += tmp.substring(tmp.length() - 4);
}
return bString;
}
private static String[] binaryArray =
{"0000","0001","0010","0011",
"0100","0101","0110","0111",
"1000","1001","1010","1011",
"1100","1101","1110","1111"};
public static String bytes2BinaryStr(byte[] bArray) {
String outStr = "";
int pos = 0;
for (byte b : bArray) {
pos = (b & 0xF0) >> 4;
outStr += binaryArray[pos];
pos = b & 0x0F;
outStr += binaryArray[pos];
}
return outStr;
}
public static byte[] string2bytes(String input) {
StringBuilder in = new StringBuilder(input);
int remainder = in.length() % 8;
if (remainder > 0)
for (int i = 0; i < 8 - remainder; i++)
in.append("0");
byte[] bts = new byte[in.length() / 8];
for (int i = 0; i < bts.length; i++)
bts[i] = (byte) Integer.parseInt(in.substring(i * 8, i * 8 + 8), 2);
return bts;
}
public static void main(String[] args) {
int i = Integer.parseInt("0010000", 2);
String s = Integer.toHexString(i);
System.out.println(s);
}
}