public class NumberUtil {
/**
* 把int转换成两字节的bin,低字节在前
*
* @param d int数值
* @return int转换成的两字节bin,低字节在前
*/
public static byte[] intTo2BinTurn(int d) {
byte[] b = new byte[2];
b[0] = (byte) ((short) d & 255);
b[1] = (byte) ((short) d >> 8);
return b;
}
/**
* 把int转换成两字节的bin
*
* @param d int数值
* @return int转换成的两字节bin
*/
public static byte[] intTo2Bin(int d) {
byte[] b = new byte[2];
b[0] = (byte) ((short) d >> 8);
b[1] = (byte) ((short) d & 255);
return b;
}
/**
* 从一个两字节的bin结构返回int值(考虑符号位影响) 低字节在前,高字节在后
*
* @param frame
* @param offset
* @return
*/
public static int parse2BytesBin(byte[] frame, int offset) {
int a = frame[offset] < 0 ? frame[offset] + 256 : frame[offset];
int b = frame[offset + 1] < 0 ? frame[offset + 1] + 256 : frame[offset + 1];
return a + (b << 8);
}
/**
* 16进制转10进制
*
* @param info
* @return
*/
public static int hexToInt(String info) {
BigInteger bbb = new BigInteger(info, 16);
int value = bbb.intValue();
return value;
}
/**
* 10进制转16进制
*/
public static String intToHex(int i) {
String hexStr = null;
if (i<16){
hexStr = "0" + Integer.toHexString(i).toUpperCase();
} else {
hexStr = Integer.toHexString(i).toUpperCase();
}
return hexStr;
}
public static long hexToLong(String info) {
BigInteger bbb = new BigInteger(info, 16);
long value = bbb.longValue();
return value;
}
/**
* 16进制转2进制
*
* @param hexStr
* @return
*/
public static String hexTo2Str(String hexStr, int len) {
//16进制转10进制
BigInteger sint = new BigInteger(hexStr, 16);
//10进制转2进制
String result = sint.toString(2);
int l = result.length();
if (l < len) {
for (int j = 0; j < len - l; j++) {
result = "0" + result;
}
}
return result;
}
public static void main(String[] args) {
System.out.println(hexToInt("000D"));
}
}
java工具类-进制转换
最新推荐文章于 2024-07-16 15:03:07 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)