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实现多功能科学计算器(包括进制转换,三角函数,四则运算等)
- 使用栈实现进制转换(java大数进制转换)
- 数据结构实验之栈一:进制转换(java实现)
- 黑马程序员——Java基础语法:利用位运算实现的基础进制转换
- Java day03 进制转换(3)索引表 数组实现 十进制向各进制转换
- Java实现任意进制转换
- CSDN编程挑战:进制转换--Java实现
- java实现进制转换
- 利用Java5泛型特性实现泛型构件——简单的泛型类和接口
- 【数据结构】单链表的倒序、删除相同结点、按值排序等简单操作用java实现
- java实现的18位身份证格式验证算法-Java基础-Java-编程开发
- java实现链表
- OpenCV4Android JavaCameraView实现
- 黑马程序员——java小结_005_“多继承”(接口的多实现)
- JAVA中implements实现多接口
- java栈的实现
- leetcode 140:Word Break II 采用很巧妙的动态规划和DFS联合使用 JAVA源代码实现
- java list去重操作实现方式
- java使用POI实现excel文件的读取,兼容后缀名xls和xlsx
- java实现日期转中文大写形式