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