黑马程序员技术交流社区

标题: 进制的优化 [打印本页]

作者: a80C51    时间: 2015-8-29 23:45
标题: 进制的优化
  1. public class myToHex
  2. {
  3.         static char[] myCode = {
  4.                 '0','1','2','3',
  5.                 '4','5','6','7',
  6.                 '8','9','A','B',
  7.                 'C','D','E','F',
  8.         };
  9.        
  10.         public static String toHex(int originalData)
  11.         {
  12.                 char[] myTempResult = new char[8];
  13.                 int pos = myTempResult.length;
  14.                
  15.                 while(originalData != 0)
  16.                 {
  17.                         int temp = originalData & 0x0F;
  18.                        
  19.                         myTempResult[--pos] = myCode[temp];
  20.                        
  21.                         originalData>>>=4;
  22.                 }
  23.                
  24.                 String tempString = "";
  25.                
  26.                 for(int i=pos;pos<myTempResult.length;pos++)
  27.                         tempString = tempString + myTempResult[pos];
  28.                        
  29.                 return tempString;
  30.         }
  31.        
  32.         public static void myPrint(String result)
  33.         {
  34.                 System.out.println("the result is 0x"+ result + ";");
  35.         }
  36.        
  37.         public static void main(String[] args)
  38.         {
  39.                 int originalData = 6;
  40.                
  41.                 String resultString = toHex(60);
  42.                
  43.                 myPrint(resultString);
  44.         }
  45. }
复制代码
其核心是按照毕老师的思想,只是稍作优化。难点主要在于,如何进行数据的反转与存储。然后,数据的打印顺序!






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2