本文介紹了如何將十六進制字符串轉(zhuǎn)換為Java中的浮點數(shù)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
Java中如何將十六進制字符串轉(zhuǎn)換為單精度浮點數(shù)?
How to convert hexadecimal string to single precision floating point in Java?
例如如何實現(xiàn):
float f = HexStringToFloat("BF800000");//f 現(xiàn)在應(yīng)該包含 -1.0
float f = HexStringToFloat("BF800000"); // f should now contain -1.0
我問這個是因為我試過了:
I ask this because I have tried:
float f = (float)(-1.0);
String s = String.format("%08x", Float.floatToRawIntBits(f));
f = Float.intBitsToFloat(Integer.valueOf(s,16).intValue());
但我得到以下異常:
java.lang.NumberFormatException:對于輸入字符串:bf800000"
java.lang.NumberFormatException: For input string: "bf800000"
推薦答案
public class Test {
public static void main (String[] args) {
String myString = "BF800000";
Long i = Long.parseLong(myString, 16);
Float f = Float.intBitsToFloat(i.intValue());
System.out.println(f);
System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
}
}
這篇關(guān)于如何將十六進制字符串轉(zhuǎn)換為Java中的浮點數(shù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!