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
Solved

String to Char[ ]

Posted on 1998-11-16
5
218 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

791 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