![](/img/trans.png)
[英]Convert ATL::CString to std::vector<unsigned char>
[英]How to correctly convert unsigned char to CString and once again, reversely convert to the result of CString to unsigned char?
我有将unsigned char转换为CString的问题,反过来,将转换后的CString的结果转换为unsigned char。
首先,我尝试使用CString.append();.
///////////////////////////
CString result2;
CString result3;
unsigend char unchar_result[9];
unchar_result = {143, 116, 106, 224, 101, 104, 57, 157};
unchar_result[8] = NULL;
for(int i=0; i<8; i++){
result2=(char)temp[i];
result3.append(result2);
}
//////////////////////////
结果,我认为result3被正确替换了。
如果我通过ASCII码看到'unchar_result',那么unchar_result =“?tj?eh9?” 和result3是“?tj?eh9?” 太。
但我想将(CString)result3反向转换为unsigned char矩阵。
unsigned char a;
unsigned char testdecode[8];
for(int i=0; i<8 ; i++){
a=result3.GetAt(i);
testdecode[i]=a
}
转换后,结果是“?tj?eh9?” 但是,值是{63,116,106,63,101,104,57,63} ......我期望结果是{143,116,106,224,101,104,57,157}。
接下来,我再次尝试使用CStringA。
CStringA result6;
result6(unchar_result);
我可以通过调试工具的局部变量窗口看到该值。 那么,结果6是“뢶j?h9앀儆?” 此外,int len = result6.GetLength()= 13。
如何正确地将unsigned char转换为CString并做反向?
转换为CString非常简单,只需将unsigned char*
传递给c'tor即可。 从CString
到unsigned char*
转换是一项更多的工作,见下文。
unsigned char orig[] = "hello world";
std::cout << orig << " (unsigned char *)" << std::endl;
// Convert to a CString
CString cstring(orig);
std::cout << cstring << " (CString)" << std::endl;
// Convert to a unsigned char*
const size_t newsize = (cstring.GetLength() + 1);
unsigned char* nstring = new unsigned char[newsize];
strcpy_s((char*)nstring, newsize, cstring);
std::cout << nstring << " (unsigned char*)" << std::endl;
看一下文档,你可以从指向char缓冲区MSDN的指针构造CString。 对于CString类的所有支持操作 ,您可以使用GetBuffer函数来获取指向CString内部缓冲区的指针,调用ReleaseBuffer来获取此缓冲区的所有权。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.