复制代码
- 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());
- }
- }
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |