您的位置:首页 > 职场人生

黑马程序员——数组转换(二进制-八进制-十六进制)-第6天

2015-08-08 02:34 561 查看
----------- android培训java培训java学习型技术博客、期待与您交流! ------------

数组转换(二进制-八进制-十六进制)

1、数组转换(十进制-二进制)

需求:十进制转换成二进制,并打印出来。

class test

{

publicstatic void main(String[] args)

{

tobin(6);

}

/*十进制转换成二进制*/

publicstatic void tobin(int num)

{

while(num>0)//这里的局限性是数字大于0

{

system.out.println(num%2);//取余,并显示

num= num / 2;//取下一位。

}

}

}

//打印:

0

1

1

通过StringBuffer()内部函数实现:

class test

{

publicstatic void main(String[] args)

{

tobin(6);

}

/*十进制转换成二进制*/

publicstatic void tobin(int num)

{

StringBuffersb = new StringBuffer();

while(num>0)

{

//system.out.println(num%2);

sb.append(num%2);

num= num / 2;

}

}

}

//打印:

0

1

1

再对比:

class test

{

publicstatic void main(String[] args)

{

tobin(6);

}

/*十进制转换成二进制*/

publicstatic void tobin(int num)

{//StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象

StringBuffersb = new StringBuffer();//这样初始化出的StringBuffer对象是一个空的对象。即把sb存放在一个容器。而这个容器可以存放字符串。

while(num>0)

{

//system.out.println(num%2);

sb.append(num%2);/*append该方法的作用是追加内容到当前StringBuffer对象的末尾,类似于字符串连接。调用该方法以后,StringBuffer对象的内容也发生改变,例如:

StringBuffersb = new StringBuffer(“abc”);

sb.append(true);//则对象sb的值将变成”abctrue”。*/

num= num / 2;

}

system.out.println(sb.reverse());//reverse用于字符串反转.原本是011,反转变成110

}

}

//打印:

110

精简版!!!十进制转换成二进制

Dec2Bin(int num){

while(num/2>0){

sb.append(num%2);

sb.reverse();

2、数组(查表法:十进制-十六进制)

//查表法:将所以的元素临时存储起来,建立对应关系。

每一次&15后的值作为索引去查建立好的表。就可以找对应的元素。

0 1 2 3 4 5 6 7 8 9 A B C D E F(16进制)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15(10进制,也相当于角标)

这个表怎么建立呢?

可以通过数组的形式来定义。

class test

{

publicstatic void main(String[] args)

{

tohex(60);

}

publicstatic void tohex(int num)

{

char[]chs={'0','1','2','3','4','5','6','7','8','9','A','B'.'C','D','E','F'};

for(intx=0;x<8;x++)

{

inttemp=num&15;//把temp强制转换成了int型。取num的低四位

system.out.println(chs[temp]);//temp作为角标,对应数组中的字符。

num=num>>>4;//强制高位补0

}

}

}

//打印:

C

3

0

0

0

0

0

0

发现终于出结果了。但是是反着的。想要正过来呢?可以通过StringBuffer reverse功能实现。

但是这个工具还没有学习。

所以可以使用已经学习过的容器:数组来完成存储。

class test

{

publicstatic void main(String[] args)

{

tohex(60);

}

publicstatic void tohex(int num)

{

char[]chs={'0','1','2','3','4','5','6','7','8','9','A','B'.'C','D','E','F'};

//定义一个临时容器。可以存储8个字符,

char[]arr=new char[8];

for(intx=0;x<8;x++)

{

inttemp=num&15;

//system.out.println(chs[temp]);

arr[x]=chs[temp];

num=num>>>4;

}

}

//把存储数据的arr数据遍历一下。

for(intx=0;x<arr.length;x++)

{

system.out.print(arr[x]+",");

}

}

//打印:

C,3,0,0,0,0,0,0,

发现终于出结果了。但是是反着的。想要正过来呢?

class test

{

publicstatic void main(String[] args)

{

tohex(60);

}

publicstatic void tohex(int num)

{

char[]chs={'0','1','2','3','4','5','6','7','8','9','A','B'.'C','D','E','F'};

char[]arr=new char[8];

for(intx=0;x<8;x++)

{

inttemp=num&15;

arr[x]=chs[temp];

num=num>>>4;

}

}

for(intx=arr.length-1;x>=0;x--)//从角标最大的开始读,相当于倒过来了的意思。

{

system.out.print(arr[x]+",");

}

}

//打印:

0,0,0,0,0,0,3,C

我不想要多余的0,怎么办?

class test

{

publicstatic void main(String[] args)

{

tohex(60);

}

publicstatic void tohex(int num)

{

char[]chs={'0','1','2','3','4','5','6','7','8','9','A','B'.'C','D','E','F'};

//定义一个临时容器。

char[]arr=new char[8];//字符数组的默认值为'\u0000',打印的时候显示成空格。

intpos=0;//指针,对应指向有效位的元素。

while(num!=0)//判断取的低四位不为0,即为有效位。

{

inttemp=num&15;

arr[pos++]=chrs[temp];//相当于arr[0]=C;然后pos++,即pos=1;

num=num>>>4;

}

}

//把存储数据的arr数据遍历一下。

for(intx=arr.length-1;x>=0;x--)

{

system.out.print(arr[x]+",");

}

}

//打印:

, ,, , , ,3,C,

怎么去掉空格的值:?

class test

{

publicstatic void main(String[] args)

{

tohex(60);

}

publicstatic void tohex(int num)

{

char[]chs={'0','1','2','3','4','5','6','7','8','9','A','B'.'C','D','E','F'};

char[]arr=new char[8];//字符数组的默认值为'\u0000',打印的时候显示成空格。

intpos=0;//指针

while(num!=0)

{

inttemp=num&15;

arr[pos++]=chrs[temp];

num=num>>>4;

}

}

//把存储数据的arr数据遍历一下。

for(intx=pos-1;x>=0;x--)// arr[pos++]执行完后,pos会自增1.所以用pos-1取得字符

{

system.out.print(arr[x]+",");

}

}

//打印:

3,C,

class test

{

publicstatic void main(String[] args)

{

tohex(60);

}

publicstatic void tohex(int num)

{

char[]chs={'0','1','2','3','4','5','6','7','8','9','A','B'.'C','D','E','F'};

//定义一个临时容器。

char[]arr=new char[8];//字符数组的默认值为'\u0000',打印的时候显示成空格。

intpos=length-1;//指针

while(num!=0)

{

inttemp=num&15;

arr[pos--]=chrs[temp];

num=num>>>4;

}

}

system.out.println("pos="+pos);

for(intx=pos;x<arr.length;x++)

{

system.out.print(arr[x]+",");

}

}

//打印:

pos=5;

,3,C,

终极版本!!!!!!!

class test

{

publicstatic void main(String[] args)

{

tohex(60);

}

publicstatic void tohex(int num)

{

char[]chs={'0','1','2','3','4','5','6','7','8','9','A','B'.'C','D','E','F'};

//定义一个临时容器。

char[]arr=new char[8];//字符数组的默认值为'\u0000',打印的时候显示成空格。

intpos=length;//指针

while(num!=0)

{

inttemp=num&15;

//system.out.println(chs[temp]);

arr[--pos]=chrs[temp];//pos先减1,变成arr[7]

num=num>>>4;

}

}

system.out.println("pos="+pos);

for(intx=pos;x<arr.length;x++)

{

system.out.print(arr[x]+",");

}

}

//打印:

pos=6;

3,C,

3、数组查表法(十进制-二进制)

class test

{

publicstatic void main(String[] args)

{

tobin(6);

}

publicstatic void tobin(int num)

{

char[]chrs={'0','1'};

//定义一个临时存储容器。

char[]arr=new char[32];//一个整数32个位,即int型,4个8位。

//定义一个操作数组的指针

intpos=arr.length;//指针

while(num!=0)

{

inttemp=num&1;//二进制&1,八进制&7,十六进制&15

arr[--pos]=chrs[temp];

num=num>>>1;

}

}

//把存储数据的arr数据遍历一下。

for(intx=0;x<arr.length;x++)

{

system.out.print(arr[x]);

}

}

//打印:

(这里代表空格) 110

4、数组(进制转换优化)

class test

{

publicstatic void main(String[] args)

{

tobin(6);

tohex(60);

toba(60);

}

//十进制转二进制

publicstatic void tobin(int num)

{

trans(num,1,1);

}

//十进制转十六进制

publicstatic void toba(int num)

{

trans(num,7,3);

}

//十进制转十六进制

publicstatic void tohex(int num)

{

trans(num,15,4);

}

publicstatic void trans(int num,int
base,int offset)//与的基数base。位移的数offset

{

if(num==0)

{

system.out.println(0);//提前判断是否为0

return0;

}

char[]chs={'0','1','2','3','4','5','6','7','8','9','A','B'.'C','D','E','F'};

//定义一个临时存储容器。

char[]arr=new char[32];//一个int型整数32个位

//定义一个操作数组的指针

intpos=arr.length;//指针

while(num!=0)

{

inttemp=num&base;

arr[--pos]=chrs[temp];

num=num>>>offset;

}

}

//把存储数据的arr数据遍历一下。

for(intx=pos;x<arr.length;x++)

{

system.out.print(arr[x]);

}

}

//打印:

110

3C

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