简体   繁体   English

如何正确地将unsigned char转换为CString并再次反向转换为CString到unsigned char的结果?

[英]How to correctly convert unsigned char to CString and once again, reversely convert to the result of CString to unsigned char?

I have problems with Converting unsigned char to CString and reversely, converting the result of converted CString to unsigned char. 我有将unsigned char转换为CString的问题,反过来,将转换后的CString的结果转换为unsigned char。

first, I try to using CString.append();. 首先,我尝试使用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);
}

////////////////////////// //////////////////////////

as a result, I think result3 was correctly substituted. 结果,我认为result3被正确替换了。

if I saw 'unchar_result' by the ASCII code, then unchar_result = "?tj?eh9?" 如果我通过ASCII码看到'unchar_result',那么unchar_result =“?tj?eh9?” and result3 was "?tj?eh9?" 和result3是“?tj?eh9?” too. 太。

But I want to reversely convert (CString)result3 to unsigned char matrix. 但我想将(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

}

after converting, the result was "?tj?eh9?" 转换后,结果是“?tj?eh9?” but, the value was { 63, 116, 106, 63, 101, 104, 57, 63}... i expect the result was { 143, 116, 106, 224, 101, 104, 57, 157 }. 但是,值是{63,116,106,63,101,104,57,63} ......我期望结果是{143,116,106,224,101,104,57,157}。

next, I try again using CStringA. 接下来,我再次尝试使用CStringA。

CStringA result6;
result6(unchar_result);

and I can see the value through the local variable window of the debug tool. 我可以通过调试工具的局部变量窗口看到该值。 then, result6 was "뢶j?h9앀儆?" 那么,结果6是“뢶j?h9앀儆?” moreover int len = result6.GetLength() = 13. 此外,int len = result6.GetLength()= 13。

How to correctly convert unsigned char to CString and to do inverse? 如何正确地将unsigned char转换为CString并做反向?

Conversion to CString is pretty easy, just pass the unsigned char* to the c'tor. 转换为CString非常简单,只需将unsigned char*传递给c'tor即可。 Conversion from CString to unsigned char* is a little more work, see below. CStringunsigned 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;

Take a look at documentation, you can construct CString from a pointer to char buffer MSDN . 看一下文档,你可以从指向char缓冲区MSDN的指针构造CString。 All supported operations for CString class , you may use GetBuffer, function to get a pointer to internal buffer of the CString, call ReleaseBuffer to take ownership of this buffer. 对于CString类的所有支持操作 ,您可以使用GetBuffer函数来获取指向CString内部缓冲区的指针,调用ReleaseBuffer来获取此缓冲区的所有权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM