Solved

char to CString

Posted on 2002-03-05
8
4,691 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

860 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