//十進制轉爲十六進制 public class ArrayTest7 { public static void main(String[] args){ System.out.println(toHex(60)); } //十進制轉爲十六進制的每一位都是十六進制元素中的某一個 //十六進制的元素有不少固定個數,並且還有對應的編號。因此能夠用查表發 public static String toHex(int num) { char[] chs = { '0','1','2','3', '4','5','6','7', '8','9','A','B', 'C','D','E','F' }; //2.建立臨時容器 char[] arr = new char[8]; //3.建立操做臨時容器的角標 int index = arr.length; //4.經過循環對num進行&>>>等運算 while(num != 0) { //5.對num進行&運算 int temp = num & 15; //6.根據&運算後的結果做爲角標查表,獲取對應的字符,並將字符存儲到臨時容器中 arr[--index] = chs[temp]; //7.對num進行右移 num = num >>>4; } return "0x"+toString(arr,index); } public static String toString(char[] arr,int index){ String temp = ""; for(int i = index;i<arr.length;i++){ temp = temp + arr[i]; } return temp; } }