Public Group

# CString to char* :(

This topic is 4947 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

more data conversion problems :( I have a url inputted through an edit box. I parse this url into hostname, filepath and filename now for the following line I need the hostname as a const char * if (inet_addr(HostName) == INADDR_NONE) I have googled and tried the GetBuffer method, but to no avail. Also can someone point me to a good unicode explanation tutorial.

##### Share on other sites
CString.operator LPCSTR() if you need a char* to use. It should work for what you are doing.

For a good unicode tutorial, take a look at this

##### Share on other sites

can you give me some example code, as im not entirely sure how to use that.

##### Share on other sites
Sure!
CString HostName = "hostname";if (inet_addr( HostName.operator LPCSTR() ) == INADDR_NONE)

I'm pretty sure that should work for you, you can also const char* cast it if you wanted to as well.
CString HostName = "hostname";if (inet_addr( (const char*)(HostName.operator LPCSTR()) ) == INADDR_NONE)

##### Share on other sites
Quote:
 Original post by Drew_BentonSure!CString HostName = "hostname";if (inet_addr( HostName.operator LPCSTR() ) == INADDR_NONE)

No need for the convoluted syntax. This should work just fine:
CString HostName = "hostname";

##### Share on other sites
In general, an operator foo for a class, where foo is an existing typename rather than some symbol describing an operator, is a "cast operator", used to perform casts to foo when they are requested. LPCSTR is simply a typedef for a char* ("long pointer to C-style string"); thus its presence in the CString definition means we can obtain a const char* by simply casting to that type, as the AP illustrates. (You could also be more explicit by writing static_cast<const char*>(HostName).)

Note that CStrings are nearly as ugly as working with char*'s directly, and you really should prefer std::string for any textual data in C++ (to the extent that it's possible - here you're working with someone else's library, so you'll have to bite the bullet). Note also that std::string does *not* provide this cast operator (but instead a member function .c_str() which performs the task), and the people who designed the library have very good reasons for having done it this way (unfortunately I can't recall at the moment what they are).

##### Share on other sites
mystring.GetBuffer(0) will give you the char*.

##### Share on other sites
ok, the

Hostname.Operatoer LPCSTR()

gives an error: "const char *: is ambiguouse ..."

the

(LPCSTR)hostname

gives the error: " can nt convert cstring to LPCSTR"

and as I said in my first post getbuffer doesn't work either I get

"can not coonvert paramater 1 from wchar _t* to const char*

##### Share on other sites
The operator in CString is named LPCTSTR. LPCSTR and LPCTSTR both (should) point to a constant null-terminated string, but LPCTSTR indicates Unicode/DBCS compatibility (if UNICODE is defined), while LPCSTR does not.

See MSDN for more details.

##### Share on other sites
neither work :(

there must be a way to do this.

A CEdit box returns a CString, and the inet_addr needs a char *

Is there any other function to replace either of these, to either start with a char[] or use a cstring?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 15
• 14
• ### Forum Statistics

• Total Topics
634070
• Total Posts
3015333
×