Solved

char to CString

Posted on 2002-03-05
8
4,695 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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!

Question has a verified solution.

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

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

627 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