Solved

String to Char[ ]

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

815 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

10 Experts available now in Live!

Get 1:1 Help Now