Cactus wrote:
How to convert unsigned char* to CString:
I wrote some function:
u_char_[0] =55;
u_char_[1] =66;
u_char_[2] =77;
........[i]=ii......
Convert_to_CS(u_char_);
****
void Convert_to_CS (unsigned char* u_char){
CString str0,str;
str0.Format("%d",u_char[0]);
AfxMessageBox ("First:" +str0);
CString str(u_char);
AfxMessageBox ("Second:" +str);
}
***
Output:
First: A
Second: // so nothing, why ??
Why working only first convetion ? I want convert whole array of uchar,
how to do it ?
Why should it work for the second since you never passed it the second?
Since you insist on a C++ solution, then consider the following:
a) an array of unsigned char is *not* terminated.
b) you therefore need to terminate it if you plan to pass it around.
Here is a solution, if you need this to work with CStrings and
AFXMessageBox, then i'ld suggest asking a relevant newsgroup since
neither is C++.
#include <iostream>
#include <string>
#include <sstream>
template< typename T, const size_t Size >
std::string Convert(T (&array)[Size])
{
std::ostringstream oss;
oss << array;
return oss.str();
}
int main(int argc, char* argv[])
{
typedef unsigned char UChar;
UChar A[] = {55, 56, 66, 77, 0};
for (size_t i = 0; i < sizeof(A)/sizeof(UChar); ++i)
{
std::cout << "A[" << i << "] = ";
std::cout << A[i] << std::endl;
}
std::cout << Convert(A) << std::endl;
}
/*
A[0] = 7
A[1] = 8
A[2] = B
A[3] = M
A[4] =
78BM
*/