参考: http://topic.csdn.net/u/20081006/16/859ded6d-7ef1-4b72-b9d8-5ce5d2d7da89.html
CString CMacroCmd::Ascii2String(CString strAscii)
{
#ifdef UNICODE
CString strValue = _T("");
TCHAR tch1 = 0x0000;
for (int i=0; i<strAscii.GetLength()/4; i++)
{
CString stemp1 = strAscii.Mid(i*4, 4);
_stscanf_s(stemp1.GetBuffer(0),_T("%x"),&tch1);!!!
CString stemp3;
stemp3.Format(_T("%c"), tch1);
strValue = strValue+stemp3;
}
return strValue;
#else
ASSERT(FALSE); // 必À?须?是º?UNICODE
return strAscii;
#endif
}
程序运行完 _stscanf_s(stemp1.GetBuffer(0),_T("%x"),&tch1);
后在离开次函数时弹出错误提示:
Run-Time Check
Failure #2 - Stack around the variable 'tch1'was corrupted.
请教高人这是怎么回事?
-----参考解答:
sscanf 是一个非常不安全的函数,定义 WORD Step 则 sizeof(Step) = 2,
VS2005 下 _stscanf_s( XX,
_T("%d"), &Step ) 会将输出参数当作 int 类型来写入, sizeof(int) 是4个字节。
这样会导致多写入2个字节,造成数据的意外破坏。
而VC6没问题应该是VC6将这个优化了或者忽略了。
解决方法:int tch1 = 0; //TCHAR tch1 = 0x0000;