黑马程序员技术交流社区
标题:
进制的优化
[打印本页]
作者:
a80C51
时间:
2015-8-29 23:45
标题:
进制的优化
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);
}
}
复制代码
其核心是按照毕老师的思想,只是稍作优化。难点主要在于,如何进行数据的反转与存储。然后,数据的打印顺序!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2