Java int 到十六进制 0x
Java int to hex with 0x
我正在尝试将 int 转换为格式为 0x12 0x2B 等的 hex。有没有类似的东西 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};
您可以声明一个 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)
我正在尝试将 int 转换为格式为 0x12 0x2B 等的 hex。有没有类似的东西 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};
您可以声明一个 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)