unsigned char* to CString - problem

C

Cactus

How to convert unsigned char* to CString:

I wrote some function:

u_char_[0] =55;
u_char_[1] =66;
u_char_[2] =77;
.........=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 ?
 
B

BobR

Cactus wrote in message ...
How to convert unsigned char* to CString:
I wrote some function:

u_char_[0] =55;
u_char_[1] =66;
u_char_[2] =77;
........=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 ?


Show us the definition for CString, AfxMessageBox.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9).
 
S

Salt_Peter

Cactus said:
How to convert unsigned char* to CString:

I wrote some function:

u_char_[0] =55;
u_char_[1] =66;
u_char_[2] =77;
........=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::eek:stringstream 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 << std::endl;
}

std::cout << Convert(A) << std::endl;
}

/*
A[0] = 7
A[1] = 8
A[2] = B
A[3] = M
A[4] =
78BM
*/
 
J

Jim Langston

Cactus said:
How to convert unsigned char* to CString:

I wrote some function:

u_char_[0] =55;
u_char_[1] =66;
u_char_[2] =77;
........=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);


CString probably doesn't have a constructor taking an unsigned char * and
converting it to a CString (whatever that is). But it seems to be accepting
it for compiling, but you'll have to check the documentation (for the non
standard) CString to figure out what it's supposed to do. You seemed to
have answered your own question since you're doing this test in the first
place.
 
D

Dan Bloomquist

Jim said:
"Cactus" <[email protected]> wrote in message

CString probably doesn't have a constructor taking an unsigned char * and
converting it to a CString (whatever that is)....

As the op is using AfxMessageBox, this is MFC. It does have such:

CSTRING_EXPLICIT CStringT( const unsigned char* pszSrc ) :
CThisSimpleString( StringTraits::GetDefaultManager() )
{
*this = reinterpret_cast< const char* >( pszSrc );
}

But as it turns out, the op is multi posting this. In an other group it
was revealed that he is not running the same code he is posting.

Best, Dan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top