A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© a80C51 中级黑马   /  2015-8-29 23:45  /  165 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  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. }
复制代码
其核心是按照毕老师的思想,只是稍作优化。难点主要在于,如何进行数据的反转与存储。然后,数据的打印顺序!

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马