Solved

char to CString

Posted on 2002-03-05
8
4,693 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

685 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