如何把CString转换为DWORD

来源:百度知道 编辑:UC知道 时间:2024/06/22 02:51:13
比如说我要转换如下的东西: CString sp("51d3fc34");
CString showbox;
DWORD show;
show=atol(sp);
showbox.Format("%08x",show);
AfxMessageBox(showbox);
上面的方法转换不成功
说明:51d3fc34这个数是一个DWORD的16进制形式。
我想要的到的结果是51d3fc34,但是结果不是这个,应该怎样转换呢?

CString sp("51d3fc34");
CString showbox;
DWORD show;
sscanf(sp,"%x",&show);
showbox.Format("%08x",show);
AfxMessageBox(showbox);

int htoi(const char* psz)
{
ASSERT(psz);
size_t nlen = ::strlen(psz);

int result = 0;
for(size_t i = 0; i < nlen; i++)
{
int digit = ctoi(*psz++);
result += int(digit*::pow(16, nlen - 1 - i));
}

return result;
}
这个函数可以直接完成你的要求。