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]


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM