您的位置:首页 > 编程语言 > Java开发

Java之查表法实现进制转换

2012-08-12 09:26 344 查看
/*
层层深入,步步为赢
*/
class BinaryTest2
{
//通过查表法,实现十进制转变成十六进制
public static void main(String[] args)
{
toHex_1(60);
toHex_2(60);
toHex_3(60);
toHex_4(60);
toHex_5(60);
toHex_6(60);
toHex_7(60);
toHex_8(60);
toHex_9(60);
}
//查表法;步骤一
public static void toHex_1(int num)
{
char[] ch = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
for (int x = 0;x<8 ;x++ )
{
int temp = num & 15;
//这里是按照角标取得的值,所以这里不需要判断
System.out.print(ch[temp]);
num = num >>> 4;
}
System.out.println();
}
//查表法:步骤二
public static void toHex_2(int num)
{
char[] ch = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
//定义一个临时容器
char[] cha = new char[8];
for (int x = 0;x<8 ;x++ )
{
/*
步骤:
1,首先将数字1111“&”一下
2,将临时数据存储到临时的容器中
3,然后在将数字右移
*/
int temp = num & 15;
//System.out.print(ch[temp]);
cha[x] = ch[temp];//将每一个最低的四位数字,计算出来,存到临时的容器当中
num = num >>> 4;
System.out.print(cha[x]);
}
System.out.println();
}
//查表法:步骤三
public static void toHex_3(int num)
{
char[] ch = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
//定义一个临时容器
char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
for (int x = 0;x<8;x++)
{
int temp = num & 15;
//System.out.print(ch[temp]);
cha[x] = ch[temp];//往里面传输数据的时候,指针是在移动的
num = num >>> 4;
}
//遍历输出临时存储容器中的所存储的东西
//将数据从最后位输出
for (int x = cha.length-1;x >= 0 ;x-- )
{
System.out.print(cha[x]+" ");
}
System.out.println();
}
//查表法:步骤四
public static void toHex_4(int num)
{
char[] ch = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
//定义一个临时容器
char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
int poster = 0;
while (num != 0)
{
int temp = num & 15;
//System.out.print(ch[temp]);
cha[poster++] = ch[temp];//往里面传输数据的时候,指针是在移动的
num = num >>> 4;
}
//遍历输出临时存储容器中的所存储的东西
//将数据从最后位输出
for (int x = cha.length-1;x >= 0 ;x-- )
{
System.out.print(cha[x]+",");
}
System.out.println();
}
//查表法:步骤五
public static void toHex_5(int num)
{
char[] ch = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
//定义一个临时容器
char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
int poster = 0;
while (num != 0)
{
int temp = num & 15;
//System.out.print(ch[temp]);
cha[poster++] = ch[temp];//往里面传输数据的时候,指针是在移动的
num = num >>> 4;
}
//遍历输出临时存储容器中的所存储的东西
//将数据从最后位输出
for (int x = poster;x >= 0 ;x-- )
{
System.out.print(cha[x]+",");
}
System.out.println();
}
//查表法:步骤六
public static void toHex_6(int num)
{
char[] ch = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
//定义一个临时容器
char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
int poster = 0;
while (num != 0)
{
int temp = num & 15;
//System.out.print(ch[temp]);
cha[poster++] = ch[temp];//往里面传输数据的时候,指针是在移动的
num = num >>> 4;
}
//遍历输出临时存储容器中的所存储的东西
//将数据从最后位输出
for (int x = poster-1;x >= 0 ;x-- )
{
System.out.print(cha[x]+",");
}
System.out.println();
}
//查表法:步骤七
public static void toHex_7(int num)
{
char[] ch = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
//定义一个临时容器
char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
int poster = cha.length-1;
while (num != 0)
{
int temp = num & 15;
//System.out.print(ch[temp]);
cha[poster--] = ch[temp];//往里面传输数据的时候,指针是在移动的
num = num >>> 4;
}
//遍历输出临时存储容器中的所存储的东西
//将数据从最后位输出
for (int x = poster;x <cha.length ;x++)
{
System.out.print(cha[x]+",");
}
System.out.println();
}
//查表法:步骤八
public static void toHex_8(int num)
{
char[] ch = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
//定义一个临时容器
char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
int poster = cha.length-1;
while (num != 0)
{
int temp = num & 15;
//System.out.print(ch[temp]);
cha[poster--] = ch[temp];//往里面传输数据的时候,指针是在移动的
num = num >>> 4;
}
//遍历输出临时存储容器中的所存储的东西
//将数据从最后位输出
for (int x = poster+1;x <cha.length ;x++)
{
System.out.print(cha[x]+",");
}
System.out.println();
}
//查表法:步骤九(最终版)
public static void toHex_9(int num)
{
char[] ch = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
//定义一个临时容器
char[] cha = new char[8];//此默认初始化的值为:"\u0000",相当于一个空位
int poster = cha.length;
while (num != 0)
{
//取出有效位
int temp = num & 15;
//System.out.print(ch[temp]);
cha[--poster] = ch[temp];//往里面传输数据的时候,指针是在移动的
num = num >>> 4;
}
//遍历输出临时存储容器中的所存储的东西
//将数据从最后位输出
for (int x = poster;x <cha.length ;x++)
{
System.out.print(cha[x]+",");
}
System.out.println();
}
}


   在这里我给大家演示的是实现十进制转变成十六进制步骤,其他的进制转换都一样,不多说了,给大家演示一下

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 存储 c string