Jump to content
  • Advertisement
Sign in to follow this  
crazy_andy

CString to char* :(

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Drew_Benton
Sure!

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";
if (inet_addr( (LPCSTR)HostName ) == INADDR_NONE)

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!