CString to char* :(

Started by
11 comments, last by crazy_andy 18 years, 9 months ago
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.
www.stickskate.com -> check it out, some gnarly stick skating movies
Advertisement
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
thanks for the quick reply.

can you give me some example code, as im not entirely sure how to use that.
www.stickskate.com -> check it out, some gnarly stick skating movies
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)
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)
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).
mystring.GetBuffer(0) will give you the char*.
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*
www.stickskate.com -> check it out, some gnarly stick skating movies
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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?
www.stickskate.com -> check it out, some gnarly stick skating movies

This topic is closed to new replies.

Advertisement