java实现16进制字符串转float浮点数、字节数组和16进制字符串相互转换

1.16进制字符串转float浮点数

String str = "415C568C";
BigInteger b = new BigInteger(str, 16);
float value = Float.intBitsToFloat(b.intValue());
System.out.println(value);

输出:13.77113

2.字节数组转16进制字符串

byte[] bytes = new byte[4];
bytes[0] = 60;
bytes[1] = 35;
bytes[2] = -41;
bytes[3] = 10;
String str = Hex.encodeHexString(bytes);
System.out.println(str);

输出:3c23d70a

3.16进制字符串转字节数组

String hexStr = "3c23d70a";
byte[] bytes = new byte[hexStr.length() / 2];
for (int i = 0; i < bytes.length; i++) {
    bytes[i] = (byte) (0xff & Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16));
}
System.out.println(Arrays.toString(bytes));

输出:[60, 35, -41, 10]

posted @ 2022-04-14 11:37  coder小白  阅读(3680)  评论(0)    收藏  举报