本帖最后由 邵阳 于 2012-6-23 10:05 编辑
今天练习发现- /*查表法,通过查表十进制转十六进制,思路是建立一个包含十六进制的各个字符的数组。然后通过数组直接转化。*/
- class Shao
- {
- public static void main(String[] args)
- {
- shao_1(60);
- }
- public static void shao_1(int key)
- {
- char []chs={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E'};
- char[]chr=new char[8];
- //for (int x=0;x<8;x++ )
- //int pose=0;
- while (key!=0)
- {
- <FONT color=red>int pose=0;
- </FONT>
- int temp=key&15;
- //System.out.println(chs[temp]);
- chr[pose++]=chs[temp];
- key=key>>>4;
- }
- //for (int x=chr.length-1;x>=0 ;x-- )
- for (int x=0;x<chr.length-1 ;x++ )
- {
- System.out.print(chr[x]+",");
- }
- }
- }
复制代码 结果是3,, , , , , ,
如果将int pose=0 放在while外面,代码如下- /*查表法,通过查表十进制转十六进制,思路是建立一个包含十六进制的各个字符的数组。然后通过数组直接转化。*/
- class Shao
- {
- public static void main(String[] args)
- {
- shao_1(60);
- }
- public static void shao_1(int key)
- {
- char []chs={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E'};
- char[]chr=new char[8];
- //for (int x=0;x<8;x++ )
- <FONT color=red>int pose=0;
- </FONT>while (key!=0)
- {
- //int pose=0;
- int temp=key&15;
- //System.out.println(chs[temp]);
- chr[pose++]=chs[temp];
- key=key>>>4;
- }
- //for (int x=chr.length-1;x>=0 ;x-- )
- for (int x=0;x<chr.length-1 ;x++ )
- {
- System.out.print(chr[x]+",");
- }
- }
- }
复制代码 得到结果是C,3,,,,,,
为什么两次得的结果不一样,郁闷
|
|