Solved

char to CString

Posted on 2002-03-05
8
4,688 Views
Last Modified: 2012-05-04
How can I convert a array of chars to a CString?
for example:
char* buf[50]
CString myString;

myString = myString + charTostr(buf);
0
Comment
Question by:el_rooky
8 Comments
 
LVL 4

Expert Comment

by:pagladasu
ID: 6842611
myString = buff; //converts char* to CString;

0
 

Author Comment

by:el_rooky
ID: 6842708
When I do that I get the following error:

error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'char *[2052]' (or there is no acceptable conversion)

I am using VC++6 and MFC
thanks
0
 
LVL 3

Expert Comment

by:jcgd
ID: 6842818
myString.Format("%s",buf);
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6842830


    char buf[50]
    CString myString;
    ...

    myString = buf;

 
or

    char *buf[50] // array of pointer
    CString myString;
    ...

    myString = *buf;

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:dchan_4544
ID: 6842906
First of all, are you trying to convert an array of chars or an array of "strings" ? (according to your example, you have char* buf[50], which is an array of strings (buf[50] is a string or array of characters)).  So, if you are just trying to convert an array of characters, then maybe you can do this:

char buf[50];
CString mystring;

strncpy(buf, "Bla Bla", sizeof(buf));
mystring = buf;

If you have an array of "strings" (array of array of characters), then maybe you can do this:

char* buf[50];
CString mystring;

buf[0] = new char[50];
strncpy(buf[0], "bla Bla", 50);
mystring = buf[0];
delete buf[0];

Hope that helps.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6842917
You should always initialize your variables when you declare them.

For example:
const char* buf = "Hello World";
CString myString = "Hey";

myString = myString + buf;

AfxMessageBox(myString);
0
 
LVL 4

Accepted Solution

by:
pagladasu earned 50 total points
ID: 6842981
Change the declaration of
char *buf[50];
to
char buf[50];
0
 

Author Comment

by:el_rooky
ID: 6844041
Thanks, I wasn't thinking yesterday.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now