如何正确地将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?
问题描述
我在将未签名的char转换为CString并将其反向转换,将转换后的CString的结果转换为unsigned char时遇到问题。
I have problems with Converting unsigned char to CString and reversely, converting the result of converted CString to unsigned char.
首先,我尝试使用CString.append( );
first, I try to using 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被正确替换。
as a result, I think result3 was correctly substituted.
如果我通过ASCII代码看到 unchar_result,则unchar_result =?tj?eh9?结果为?tj?eh9?
if I saw 'unchar_result' by the ASCII code, then unchar_result = "?tj?eh9?" and result3 was "?tj?eh9?" too.
但是我想将(CString)result3反向转换为无符号字符矩阵。
But I want to reversely convert (CString)result3 to unsigned char matrix.
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}。
after converting, the result was "?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 }.
下一步,我再次尝试使用CStringA。
next, I try again using CStringA.
CStringA result6;
result6(unchar_result);
我可以通过调试工具的局部变量窗口看到该值。然后,结果6是뢶j?h9앀儆?而且int len = result6.GetLength()= 13。
and I can see the value through the local variable window of the debug tool. then, result6 was "뢶j?h9앀儆?" moreover int len = result6.GetLength() = 13.
如何正确将unsigned char转换为CString并进行逆运算?
How to correctly convert unsigned char to CString and to do inverse?
推荐答案
转换为CString非常容易,只需将 unsigned char *
传递给c'tor。从 CString
到 unsigned char *
的转换要花点功夫,请参见下文。
Conversion to CString is pretty easy, just pass the unsigned char*
to the c'tor. Conversion from CString
to unsigned char*
is a little more work, see below.
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;
这篇关于如何正确地将unsigned char转换为CString,然后再次将CString的结果反向转换为unsigned char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!