本文介紹了如何將十六進(jìn)制字符串轉(zhuǎn)換為Java中的浮點(diǎn)數(shù)?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
Java中如何將十六進(jìn)制字符串轉(zhuǎn)換為單精度浮點(diǎn)數(shù)?
How to convert hexadecimal string to single precision floating point in Java?
例如如何實(shí)現(xiàn):
float f = HexStringToFloat("BF800000");//f 現(xiàn)在應(yīng)該包含 -1.0
float f = HexStringToFloat("BF800000"); // f should now contain -1.0
我問(wèn)這個(gè)是因?yàn)槲以囘^(guò)了:
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:對(duì)于輸入字符串: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)于如何將十六進(jìn)制字符串轉(zhuǎn)換為Java中的浮點(diǎn)數(shù)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!