Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > unsigned char* to CString - problem

Reply
Thread Tools

unsigned char* to CString - problem

 
 
Cactus
Guest
Posts: n/a
 
      11-13-2006
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 ?

 
Reply With Quote
 
 
 
 
BobR
Guest
Posts: n/a
 
      11-13-2006

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


 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      11-13-2006

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: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[i] << std::endl;
}

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

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

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      11-14-2006

"Cactus" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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 ?
>



 
Reply With Quote
 
Dan Bloomquist
Guest
Posts: n/a
 
      11-14-2006


Jim Langston wrote:
> "Cactus" <(E-Mail Removed)> wrote 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
unsigned long to unsigned char ashtonn@gmail.com Python 1 06-01-2005 07:00 PM
comparing unsigned long and unsigned int sridhar C Programming 6 11-03-2004 03:52 AM
unsigned int const does not match const unsigned int Timo Freiberger C++ 3 10-30-2004 07:02 PM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments