Solved

String to Char[ ]

Posted on 1998-11-16
5
220 Views
Last Modified: 2010-04-01
Is it possible to convert a string into a char array?  I have tried strcpy() and many other functions with no success.
0
Comment
Question by:Chang
[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
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:V245
ID: 1178009
Try something like this:

      CString Str = "Test STring";
      char szStr[32];
      strcpy((char*)szStr, LPCTSTR(Str));

0
 
LVL 22

Expert Comment

by:nietod
ID: 1178010
CString in an MFC class.  Do you mean the STL string class called "string"?  If not one of those, what do you mean by "string" ?
0
 

Author Comment

by:Chang
ID: 1178011
I'm sorry, for not phrasing my question properly.  I thought there might be a way to convert all string types to a char[] type.  I have raised the points for your troubles, sorry.

I want to convert a an LPTSTR to a WCHAR, here are my 2 variables in my program:
WCHAR LPtemp[500];
LPTSTR fName;


0
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
ID: 1178012
You can use the wide character string to multibyte character string conversion function, it is called wcstombs() and is defined in stdlib.h.

Now if the wide character string contains any characters that are not ASCII characters, the converted string will have contain multibyte characters in those places.  The OS will not be expecting those multi-byte characters in the string so it may cause problems.  Another approach that would let you avoide that problem (in favor of another) is to convert each character in the wide character string to a multibyte character using wctomb().  Then if you come to any characters that don't convert to a 1 byte multibyte character (ASCII), you can skip them.  Of course either way, the converted string is not exactly what the original was, but there is no way around that as a LPSTR type string can only store ASCII characters.
0
 

Author Comment

by:Chang
ID: 1178013
I figured out the problem, it was an extreemly simple solution compared to what I was trying to do.  I do not think the conversion I was trying is even possible.  Your answer did however lead me in the right direction, thank you very much for your help nietod!
0

Featured Post

Independent Software Vendors: 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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

729 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