public static String toHex(int x) {
if (x > 0xFFFFFF || x < 0) {
throw new IllegalArgumentException();
}
String hex = Integer.toHexString(x);
char[] chars = "00 00 00".toCharArray();
for (int i = 0; i < hex.length(); i++) {
int pos = 7 - ((i >> 1) * 3 + (i & 1));
chars[pos] = hex.charAt(hex.length() - 1 - i);
}
return new String(chars);
}
public static void main(String[] args){
System.out.println(toHex(100));
if (x > 0xFFFFFF || x < 0) {
throw new IllegalArgumentException();
}
String hex = Integer.toHexString(x);
char[] chars = "00 00 00".toCharArray();
for (int i = 0; i < hex.length(); i++) {
int pos = 7 - ((i >> 1) * 3 + (i & 1));
chars[pos] = hex.charAt(hex.length() - 1 - i);
}
return new String(chars);
}
public static void main(String[] args){
System.out.println(toHex(100));
}
结果如下:
00 00 64