本文介紹了如何將十六進制字符串轉換為Java中的浮點數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
Java中如何將十六進制字符串轉換為單精度浮點數?
How to convert hexadecimal string to single precision floating point in Java?
例如如何實現:
float f = HexStringToFloat("BF800000");//f 現在應該包含 -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)));
}
}
這篇關于如何將十六進制字符串轉換為Java中的浮點數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!