void StrDec2BufDec(const StrVal& strDec, BYTE* bufDec) { for (size_t i = 0; i < strDec.length(); ++i) { bufDec[i] = 0; if (strDec[i] >= L'0' && strDec[i] <= L'9') bufDec[i] = strDec[i] - L'0'; } } int StrDec2HexBuf( const StrVal& strDec, BYTE* destBuf ) { size_t nIndex = 0; while (nIndex < strDec.length() && (strDec[nIndex] <= '0' || strDec[nIndex] > '9')) ++nIndex; if (nIndex >= strDec.length()) return 0; size_t nDecLen = strDec.length() - nIndex; BYTE* decBuf = new BYTE[nDecLen+1]; memset(decBuf, 0, nDecLen+1); StrDec2BufDec(StrVal(strDec, nIndex), decBuf); BYTE* hexBuf = new BYTE[nDecLen]; memset(hexBuf, 0, nDecLen); BYTE nTemp = 0; for (size_t nDecIndex = 0; nDecIndex < nDecLen; ++nDecIndex) { nTemp = 0; for (int nBitIndex = 0; nBitIndex < 8; ++nBitIndex) { for (size_t nStartIndex = nDecIndex; nStartIndex < nDecLen; ++nStartIndex) { decBuf[nStartIndex+1] += (decBuf[nStartIndex]&0x01) == 1?10:0; decBuf[nStartIndex] >>= 1; } nTemp |= decBuf[nDecLen] > 0? (1 << nBitIndex):0; decBuf[nDecLen] = 0; } hexBuf[nDecIndex] = nTemp; } while (nDecLen > 0) { if (hexBuf[--nDecLen] > 0) break; } if (destBuf != NULL) memcpy(destBuf, hexBuf, nDecLen+1); delete [] decBuf; delete [] hexBuf; return nDecLen + 1; }
以上是10进制转16进制的代码,可以调用int StrDec2HexBuf( const StrVal& strDec, BYTE* destBuf ),将strDec的十进制数据转换成16进制的destBuf字符串,并返回destBuf的有效长度
关于10进制转16进制的方法:
1、我这里先将strDec转换成以每个字节代表一个数字的方法表示的方式。然后再进行相应的转换。void StrDec2BufDec(const StrVal& strDec, BYTE* bufDec)
2、关于十进制转16进制,我是将十进制转换成2进制的方式。即对每个数字进行除2取余的方式。具体关于十进制转16进制方法很多,我用转换成2进制只是其中一种方法。
#include <string>
typedef std::wstring StrVal;
int CalcStep(byte* cur, int nCurLen) { int nTemp = 0; for (int nIndex = 0; nIndex < nCurLen; ++nIndex) { nTemp += cur[nIndex] * 256; cur[nIndex] = nTemp % 10; nTemp /= 10; } while (nTemp > 0) { cur[nCurLen++] = nTemp % 10; nTemp /= 10; } return nCurLen; } void MulOneByte(byte* dec, int data, byte* base, int nBaseLen) { for (int nBaseIndex = 0; nBaseIndex < nBaseLen; ++nBaseIndex) { int nTemp = data * base[nBaseIndex]; for (int nDecIndex = nBaseIndex; nTemp > 0; ++nDecIndex) { nTemp += dec[nDecIndex]; dec[nDecIndex] = nTemp % 10; nTemp /= 10; } } } void Hex2Dec(byte* hexBuf, int nHexLen, byte* decBuf) { byte* base = new byte[nHexLen*4]; memset(base, 0, nHexLen*4); base[0] = 1; int nCurBaseLen = 1; for (int nHexIndex = 0; nHexIndex < nHexLen; ++nHexIndex) { MulOneByte (decBuf, hexBuf[nHexIndex], base, nCurBaseLen); nCurBaseLen = CalcStep(base, nCurBaseLen); } } ////////////////////////////////////////////////////////////////////////// StrVal Convert2DecData(BYTE* srcData, int nLen) { StrVal destStr; int nDecLen = nLen*4; BYTE* decData = new BYTE[nDecLen]; memset(decData, 0, nDecLen); Hex2Dec(srcData, nLen, decData); while (nDecLen-- > 0) { if (decData[nDecLen] > 0) break; } wchar_t midBuf[MAX_PATH]; while (nDecLen >= 0) { swprintf_s(midBuf, MAX_PATH, L"%d", decData[nDecLen--]); destStr.append(midBuf); } delete [] decData; return destStr; }
上面代码是将16进制转成10进制的字符串
在void Hex2Dec(byte* hexBuf, int nHexLen, byte* decBuf)这里,我也是将结果以decBuf的每个字节代表一个0~9的数字,这里我new了一个比十六进制缓冲区大4倍的缓冲区来存放数据。(经计算3倍应该够了)
首先调用void MulOneByte(byte* dec, int data, byte* base, int nBaseLen)
我的base[]初始值为1,然后再乘以hexBuf的每个字节。计算结果再加到dec[]
CalcStep则是为base缓冲区的值(以10进制表示)乘以256
因为Hex2Dec中,hexBuf每次以一个字节的进制从低位到高位进行计算,所以每次计算完成之后需要将base乘以256,然后将base调整成每个字节以10进制表示的形式。
最终
StrVal Convert2DecData(BYTE* srcData, int nLen)函数的显示。因为我的电脑是小端模式,所以从后往前进行打印输出。
额外添加自增的函数。具体自增方式可以再修改
void IncData( BYTE* val, INT64 nIncStep ) { BYTE* pIncStep = (BYTE*)&nIncStep; int nLeft = 0; for (int nIndex = 0; nIndex < sizeof(INT64); ++nIndex) { for (int nInner = 0; nInner < 8; ++nInner) { nLeft = nLeft + (val[nIndex] & (1<<nInner)) + (pIncStep[nIndex] & (1<<nInner)); val[nIndex] &= ~(1 << nInner); val[nIndex] ^= (nLeft & (1<<nInner)); nLeft &= 1 << (nInner+1); } nLeft >>= 8; } for (int nIndex = sizeof(INT64); nLeft > 0; ++nIndex) { for (int nInner = 0; nInner < 8 && nLeft > 0; ++nInner) { nLeft = nLeft + (val[nIndex] & (1 << nInner)); val[nIndex] &= nLeft & (1 << nInner) & 0xff; nLeft &= 1 << (nInner+1); } nLeft >>= 8; } }