473,414 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

unsigned char* to CString - problem

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
4 11000

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

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

"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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Toro | last post by:
1) say, CString b; char * a; how do I assign b into a ? strcpy(a,b) has no problem in compiling but I don't get what I want. 2) I used char ** argv
9
by: Fausto Lopez | last post by:
I'm getting the following error: 'strlen' : cannot convert parameter 1 from 'class CString' to 'const char *' when I try to compile the following code: HRESULT AnsiToUnicode(CString pszA,...
5
by: Tim Wong | last post by:
All: I am trying to convert a CString value to an unsigned char array. I found some code online that will allow me to compile, but when I try to print out...i get a whole mess. /*Begin Code*/...
3
by: nsyforce | last post by:
What is the correct way to convert a const char* to a CString? I'm somewhat of a newbie and have tried several ways. While they all convert ok, I'm using a profiler that shows a memory leak for...
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
1
by: nd3r | last post by:
Hy! The problem, is that I tried everything I could to convert. The following examples are already useless: //Example 1: CString text; char *ch_text = new char; strcpy(ch_text,text);
3
by: codercoder | last post by:
It is a C++ questions: How to Convert unsigned char to CString and char *; and backward as well?
6
by: meisterbartsch | last post by:
Hi NG, I have got a starting pointer of a char series (mxArrayToString(pa)). I also know the length (number of elements by mxGetNumberOfElements) I want to read. How do I get a Cstring from...
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.