将十进制数转化为定长的十六进制数
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.zzzyk.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
补充:软件开发 , C++ ,