[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

String to Char[ ]

Posted on 1998-11-16
5
Medium Priority
?
225 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 60 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.
Suggested Courses

865 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