Solved

String to Char[ ]

Posted on 1998-11-16
5
212 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
  • 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

708 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

15 Experts available now in Live!

Get 1:1 Help Now