本文介紹了在Java中將十六進制字符串轉換為字節的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
在 Java 中,如何將字節的十六進制字符串表示形式(例如1e")轉換為字節值?
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);
但這僅適用于高達 127 的值,由于有符號/無符號問題,高于需要轉換為字節的值所以我建議將其傳輸到 int 然后將其轉換為 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);
這篇關于在Java中將十六進制字符串轉換為字節的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!