By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,345 Members | 1,791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,345 IT Pros & Developers. It's quick & easy.

unsigned char* to CString - problem

P: n/a
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 ?

Nov 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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;
........[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 ?
Show us the definition for CString, AfxMessageBox.

http://www.parashift.com/c++-faq-lit....html#faq-5.9).

--
Bob R
POVrookie
Nov 13 '06 #2

P: n/a

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
*/

Nov 13 '06 #3

P: n/a

"Cactus" <ca******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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);
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.
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 ?

Nov 14 '06 #4

P: n/a


Jim Langston wrote:
"Cactus" <ca******@gmail.comwrote in message
<snip code>
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.

Nov 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.