在Java中将整数转换为十六进制字符串
Java中的Integer.toHexString()方法将Integer转换为十六进制字符串。
假设以下是我们的整数值。
int val1 = 5; int val2 = 7; int val3 = 13;
将上述int值转换为十六进制字符串。
Integer.toHexString(val1); Integer.toHexString(val2); Integer.toHexString(val3);
示例
public class Demo { public static void main(String[] args) { int val1 = 5; int val2 = 7; int val3 = 13; int val4 = 23; int val5 = 30; System.out.println("Converting integer "+val1+" to Hex String: "+Integer.toHexString(val1)); System.out.println("Converting integer "+val2+" to Hex String: "+Integer.toHexString(val2)); System.out.println("Converting integer "+val3+" to Hex String: "+Integer.toHexString(val3)); System.out.println("Converting integer "+val4+" to Hex String: "+Integer.toHexString(val4)); System.out.println("Converting integer "+val5+" to Hex String: "+Integer.toHexString(val5)); } }
输出结果
Converting integer 5 to Hex String: 5 Converting integer 7 to Hex String: 7 Converting integer 13 to Hex String: d Converting integer 23 to Hex String: 17 Converting integer 30 to Hex String: 1e