1 unsigned char* CGetHexStringDlg::TQ_GetHexStringEx(unsigned int nNumber, unsigned int nCount)
2 {
3 unsigned char* pBuff = new unsigned char[nCount];
4 memset(pBuff, '0', nCount); //所有位置'0'
5
6 static const char hex_map[] = "0123456789ABCDEF";
7
8 int nRemainder = 0; //除16的余數
9 int nConsult = 0; //除16的商
10 int nPos = nCount - 1; //指針位置
11 int nTimes = 0; //運算次數
12
13 while (nPos >= 0)
14 {
15 nRemainder = nNumber%16;
16 nConsult = nNumber/16;
17
18 if (nRemainder == 0) //余數為0
19 {
20 if (nConsult != 0) //商不為0
21 {
22 if (nConsult >= 16)
23 {
24 nNumber = nNumber/16;
25 nPos--;
26 }
27 else
28 {
29 nPos--;
30 pBuff[nPos] = hex_map[nConsult];
31 break;
32 }
33 }
34 else //nNumber為0
35 {
36 if (nTimes == 0) //nNumber為0且運算次數為1
37 {
38 pBuff[nPos] = hex_map[0];
39 break;
40 }
41 else
42 break;
43 }
44 } www.2cto.com
45 else
46 {
47 pBuff[nPos] = hex_map[nRemainder];
48 nNumber = nNumber - nRemainder;
49 }
50
51 nTimes++;
52 }
53
54 pBuff[nCount] = NULL;
55
56 return pBuff;
57 }
作者:CodeMonkey