public class MathUtil {
/**
* 获取十进制数转变为四位十六进制数
*
* @param num
* @return
*/
public static String getIntToHexString(
int num) {
byte[] bytes =
intToBytes(num);
String string =
bytes2HexString(bytes);
string = string.substring(4
);
return string;
}
/**
* 将int数值转换为占四个字节的byte数组,本方法适用于(高位在前,低位在后)的顺序。
*
* @param value
* @return
*/
private static byte[] intToBytes(
int value) {
byte[] src =
new byte[4
];
src[0] = (
byte) ((value >> 24) & 0xFF
);
src[1] = (
byte) ((value >> 16) & 0xFF
);
src[2] = (
byte) ((value >> 8) & 0xFF
);
src[3] = (
byte) (value & 0xFF
);
return src;
}
private static byte[] hex = "0123456789ABCDEF"
.getBytes();
/**
* 从字节数组到十六进制字符串转换
*
* @param b
* @return
*/
public static String bytes2HexString(
byte[] b) {
byte[] buff =
new byte[2 *
b.length];
for (
int i = 0; i < b.length; i++
) {
buff[2 * i] = hex[(b[i] >> 4) & 0x0f
];
buff[2 * i + 1] = hex[b[i] & 0x0f
];
}
return new String(buff);
}
/**
* 将指定字符串src,以每两个字符分割转换为16进制形式
* 如:"2B44EFD9" –> byte[]{0x2B, 0×44, 0xEF, 0xD9}
*
* @param src String
* @return byte[]
*/
private static byte[] HexString2Bytes(String src) {
byte[] ret =
new byte[src.length() / 2
];
byte[] tmp =
src.getBytes();
for (
int i = 0; i < tmp.length / 2; i++
) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1
]);
}
return ret;
}
/**
* 将两个ASCII字符合成一个字节;
* 如:"EF"--> 0xEF
*
* @param src0 byte
* @param src1 byte
* @return byte
*/
private static byte uniteBytes(
byte src0,
byte src1) {
byte _b0 = Byte.decode("0x" +
new String(
new byte[]{src0}))
.byteValue();
_b0 = (
byte) (_b0 << 4
);
byte _b1 = Byte.decode("0x" +
new String(
new byte[]{src1}))
.byteValue();
byte ret = (
byte) (_b0 ^
_b1);
return ret;
}
private static int byte2Int(
byte[] b) {
int intValue = 0
;
for (
int i = 0; i < b.length; i++
) {
intValue += b[i] & 0xFF
;
}
return intValue;
}
public static String totalNumber(String string) {
byte[] bytes =
HexString2Bytes(string);
int asd =
byte2Int(bytes);
String s =
Integer.toHexString(asd);
if (s.length() == 3
) {
s = '0' +
s;
}
return s;
}
public static String bytesToHexString(
byte[] src) {
StringBuilder stringBuilder =
new StringBuilder(""
);
if (src ==
null || src.length <= 0
) {
return null;
}
for (
int i = 0; i < src.length; i++
) {
int v = src[i] & 0xFF
;
String hv =
Integer.toHexString(v);
if (hv.length() < 2
) {
stringBuilder.append(0
);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/**
* 二进制转16进制
*
* @param bString
* @return
*/
public static String binaryString2hexString(String bString) {
if (bString ==
null || bString.equals("") || bString.length() % 8 != 0
)
return null;
StringBuffer tmp =
new StringBuffer();
int iTmp = 0
;
for (
int i = 0; i < bString.length(); i += 4
) {
iTmp = 0
;
for (
int j = 0; j < 4; j++
) {
iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1
);
}
tmp.append(Integer.toHexString(iTmp));
}
return tmp.toString();
}
/**
* 16进制转二进制
*
* @param hexString
* @return
*/
public static String hexString2binaryString(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;
}
}
转载于:https://www.cnblogs.com/xinmengwuheng/p/5893421.html