- public class myToNums
- {
- public static void main()
- {
- int originalNums = 60;
-
- toHexOne(originalNums);
- toHexTwo(originalNums)
- }
-
- public static void toHextoHex(int originalNums)
- {
- //There are 8 bytes in a 32 bit computer for a DEC num
- for(int i = 0;i<8;i++)
- {
- //4 bits for each byte,and 4bits is 0x0F
- int temp = originalNums & 0x0F;
-
- //turn each bit to char
- if(temp >9 )
- System.out.print(""+(temp-10+'A'));
- else
- System.out.print(""+temp);
-
- //get next byte
- originalNums=originalNums>4;
- }
- System.out.println("");
- }
-
- public void toHexTwo(int originalNums)
- {
- StringBuffer sb = new StringsBuffer();
- for(int i = 0;i<8;i++)
- {
- //4 bits for each byte,and 4bits is 0x0F
- int temp = originalNums & 0x0F;
-
- //turn each bit to char
- if(temp >9 )
- sb.append(""+(temp-10+'A'));
- else
- sb.append(""+temp);
-
- //get next byte
- originalNums>>=4;
- }
-
- System.out.println(sb.reverse());
- }
-
- }
复制代码
关于它的思路,参见注释。
另外,各位睡觉之前,找找错误呗。活动下眼睛。在不运行的情况,看能找出多少错误出来?
|
|