[英]In Java, I want to take a string representing a hex and convert it to an int while preserving the “0x” prefix
[英]Java int to hex with 0x
我正在嘗試將int轉換為十六進制,格式為0x12 0x2B,依此類推。 有沒有像python類似的東西: https : //docs.python.org/2/library/functions.html#hex來完成這個或者我需要解決這個問題,有很多不必要的步驟嗎?
我需要得到以下內容:
int []hexInt={0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01};
你可以聲明一個等於"0x" + Integer.toHexString(int)
的String
"0x" + Integer.toHexString(int)
public static String intToHexStr(int i){
return "0x"+String.format("%2s", Integer.toHexString(i)).replace(' ', '0');
}
也許這已經很晚了,但這是你問題的答案:
Integer.parseInt(String.format("%X", intValue),16)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.