473,657 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11031

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::ostringstr eam 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.goo glegroups.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_EXPLICI T CStringT( const unsigned char* pszSrc ) :
CThisSimpleStri ng( StringTraits::G etDefaultManage r() )
{
*this = reinterpret_cas t< 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
3775
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
12219
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, LPOLESTR* ppszW) { ULONG cCharacters; DWORD dwError;
5
14069
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*/ CString day("01"); unsigned char testDay;
3
9134
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 every option. Here's what I have tried: const char* test; test = getMyChar(); //CString myCString((LPCTSTR)test); //CString myCString(test); CString myCString = new CString(test);
5
7783
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 of bytes. For example, the .lib contains this function: int create(int id, int scale, unsigned char *image); In the wrapper DLL I have this function:
1
3346
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
33611
by: codercoder | last post by:
It is a C++ questions: How to Convert unsigned char to CString and char *; and backward as well?
6
3465
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 those Informations? I had the Idea to do the following
29
9960
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.