- public class myToHex
- {
- static char[] myCode = {
- '0','1','2','3',
- '4','5','6','7',
- '8','9','A','B',
- 'C','D','E','F',
- };
-
- public static String toHex(int originalData)
- {
- char[] myTempResult = new char[8];
- int pos = myTempResult.length;
-
- while(originalData != 0)
- {
- int temp = originalData & 0x0F;
-
- myTempResult[--pos] = myCode[temp];
-
- originalData>>>=4;
- }
-
- String tempString = "";
-
- for(int i=pos;pos<myTempResult.length;pos++)
- tempString = tempString + myTempResult[pos];
-
- return tempString;
- }
-
- public static void myPrint(String result)
- {
- System.out.println("the result is 0x"+ result + ";");
- }
-
- public static void main(String[] args)
- {
- int originalData = 6;
-
- String resultString = toHex(60);
-
- myPrint(resultString);
- }
- }
复制代码 其核心是按照毕老师的思想,只是稍作优化。难点主要在于,如何进行数据的反转与存储。然后,数据的打印顺序!
|
|