本文介紹了在Java中將十六進(jìn)制字符串轉(zhuǎn)換為字節(jié)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
在 Java 中,如何將字節(jié)的十六進(jìn)制字符串表示形式(例如1e")轉(zhuǎn)換為字節(jié)值?
In Java, how can a hexadecimal string representation of a byte (e.g. "1e") be converted into a byte value?
例如:
byte b = ConvertHexStringToByte("1e");
推薦答案
可以使用Byte.parseByte("a", 16);
但這僅適用于高達(dá) 127 的值,由于有符號/無符號問題,高于需要轉(zhuǎn)換為字節(jié)的值所以我建議將其傳輸?shù)?int 然后將其轉(zhuǎn)換為 byte
You can use Byte.parseByte("a", 16);
but this will work only for values up to 127,
values higher then that will need to cast to byte, due to signed/unsigned issues
so i recommend to transfer it to an int and then cast it to byte
(byte) (Integer.parseInt("ef",16) & 0xff);
這篇關(guān)于在Java中將十六進(jìn)制字符串轉(zhuǎn)換為字節(jié)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!