字符串的ASCII 读写

CString CMacroCmd::String2Ascii(CString strValue)
{
#ifdef UNICODE
CString strAscii = _T("");
for (int i=0; i<strValue.GetLength(); i++)
{
CString stemp;
stemp.Format(_T("%04X"), strValue.GetAt(i));

strAscii = strAscii+stemp;
}
return strAscii;
#else
ASSERT(FALSE); // 必须是UNICODE
return strValue;
#endif
}

CString CMacroCmd::Ascii2String(CString strAscii)
{
#ifdef UNICODE
CString strValue = _T("");

int tch1 = 0; //TCHAR tch1 = 0x0000; 原因参考 http://www.cnblogs.com/carl2380/archive/2011/06/21/2086011.html
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
}

原文地址:https://www.cnblogs.com/carl2380/p/2307248.html