java编程思想练习题-第3章练习13-char的二进制

题目:编写一个方法,它以二进制形式显示char类型的值。使用多个不同的字符来展示它。

import java.util.*; 
public class test {
	
public static int transform(int  a )
{
     int temp,result;
     if(a==1)
        return 1;
     temp=a/2;
     result=(a%2)+transform(temp)*10;
     return result;
}

	public static void main(String[] args) {
		char[] chars="abcdefghijklmnopqrstuvwxyz`!@#$%^&*()_+`-=1234567890{}[]|\\:'<.,>/?".toCharArray();
		for(int i=0;i<=chars.length-1;i++){
			System.out.println(chars[i]+"="+transform(chars[i]));
		}
	}


}

 示例中用迭代的方式计算了每个char的二进制数值。

结果为:

a=1100001
b=1100010
c=1100011
d=1100100
e=1100101
f=1100110
g=1100111
h=1101000
i=1101001
j=1101010
k=1101011
l=1101100
m=1101101
n=1101110
o=1101111
p=1110000
q=1110001
r=1110010
s=1110011
t=1110100
u=1110101
v=1110110
w=1110111
x=1111000
y=1111001
z=1111010
`=1100000
!=100001
@=1000000
#=100011
$=100100
%=100101
^=1011110
&=100110
*=101010
(=101000
)=101001
_=1011111
+=101011
`=1100000
-=101101
==111101
1=110001
2=110010
3=110011
4=110100
5=110101
6=110110
7=110111
8=111000
9=111001
0=110000
{=1111011
}=1111101
[=1011011
]=1011101
|=1111100
\=1011100
:=111010
'=100111
<=111100
.=101110
,=101100
>=111110
/=101111
?=111111

对照ascii码表,会发现是一致的(废话)

猜你喜欢

转载自buptchj.iteye.com/blog/2247463