数组---进制转换(查表法)
来源:互联网 发布:ai中文版mac下载 编辑:程序博客网 时间:2024/06/14 20:27
package com.shuzu;
public class shuzuJinZhiZhuanHuan {
* @param a所要转换的十进制数,b 不同进制所要与(&)的数不同,wei 向右移动几位
* 查表法
*十进制转二进制
*十进制转十六进制
*定义数组字符表
*定义数组容器arr,长度为32位(二进制) or 8位(十六进制)
*定义指针pos
*循环传入参数(int a),判断a的值如果不等于0继续循环
*
* 60---00000000 0000 0000 0000 0000 0011 1100
*&15 0000 0000 0000 0000 0000 0000 00001111
*----------------------------------------------
* 0000 0000 0000 0000 0000 0000 0000 1100 = 12 'C'
publicstatic void main(String[] args) {
// TODO Auto-generated method stub
trans(60,15,4);
}
publicstatic void trans(int a,int b,int wei){
char[] temp = { '0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F' }; //字符表
char[] arr = newchar[32]; //定义容器,用来存储转换后的字符数组
int su = 0; //定义临时变量,用于存储与&之后的结果
int pos =arr.length; //定义指针下标,从数组的最后一位开始,用于从后往前存,将结果反转
while(a!=0){ //循环移位之后的十进制数
su = a &b; //将与&的结果存入临时变量su中
arr[--pos] =temp[su]; //temp[su],将字符表中下标位su的字符存入arr中,--pos,从数组的最后一位开始存,每次减一向前移动一位
a = a >>>wei; //将十进制数想右移动4位(十六进制) or 1位(二进制),一遍得到下一个四位的值
public class shuzuJinZhiZhuanHuan {