什么时候使用数组
如果数据出现了对应关系,而且对应关系的一方是有序的数字编号,并作为脚标使用这时就必须想到数组的使用
就可以将这些数据存储到数组中。
根据运算的结果作为脚标直接去查数组中对应的元素即可
这种方式就叫:查表法
/*
0,1,2,3,4,5,6,7,8,9,A ,B ,C ,D ,E ,F
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
*/
class Test
{
public static void main(String args[])
{
toHex(60); //输出3C
}
public static void toScale(int num,int shift,int offset)
{
char arr[] ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};//用于查表记录每次的运算值
char chs[] = new char[32];//记录最终的输出数字的数组
int pos=chs.length;
while(num!=0)
{
int n=num & shift; //shift 表示转换的进制
chs[--pos]=arr[n]; //将数据存储到
num = num >>> offset; //是原数据右移位的位数
}
for(int j=pos;j<chs.length;j++)
System.out.print(chs[j]);
}
public static void toHex(int num) //将10进制的数字转换成8进制的数字
{
toScale(num,7,3); //转成16进制就是toScale(num,15,4);
}
}