I have problems with Converting unsigned char to CString and reversely, converting the result of converted CString to unsigned char.
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);
}
//////////////////////////
as a result, I think result3 was correctly substituted.
if I saw 'unchar_result' by the ASCII code, then unchar_result = "?tj?eh9?" and result3 was "?tj?eh9?" too.
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
}
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 }.
next, I try again using 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앀儆?" moreover int len = result6.GetLength() = 13.
How to correctly convert unsigned char to CString and to do inverse?
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;