Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


string conversion


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 tadobie   Members   -  Reputation: 122

Like
0Likes
Like

Posted 11 September 2004 - 07:35 PM

Hi all, I am trying to place a path name into the CreateSurfaceFromBitmap() function using DirectDraw I need to use a string so I can create a full path name such as: string PathName = x + '/' + y + '/' + z + ".bmp"; (that part is fine but I then need to convert that to type TCHAR* so I can use it in CreateSurfaceFromBitmap() I've been looking for solutions and I'm at a loss. Any suggestions??

Sponsor:

#2 IFooBar   Members   -  Reputation: 906

Like
0Likes
Like

Posted 11 September 2004 - 07:57 PM

Use the preprocessor:



#ifdef UNICODE
typedef std::wstring tstring;
#else
typedef std::string tstring;
#endif

tstring ToTChar( const std::string& str )
{
#ifdef UNICODE
// convert string to wstring and return string
// look at mbstowcs function
#else
return str;
#endif
}

tstring ToTChar( const std::wstring& str )
{
#ifdef UNICODE
return str;
#else
// convert wstring to string and return string
// look at wcstombs function
#endif
}



#3 tadobie   Members   -  Reputation: 122

Like
0Likes
Like

Posted 11 September 2004 - 09:25 PM

Can you explain this a little further because I cant quite get my head around it. Does this return a type TCHAR* or a string?
Is wstring related to TCHAR*? Does mbstowcs let me pass in a c++ style string and return a TCHAR*?
Sorry I'm a little confused because I haven't come across these data types before.

[Edited by - tadobie on September 12, 2004 3:25:06 AM]

#4 davedx   Members   -  Reputation: 166

Like
0Likes
Like

Posted 11 September 2004 - 10:07 PM

Try:

string x = "foo" + "/" + "bar";

TCHAR* y = (TCHAR*) x.c_str();

:)

#5 IFooBar   Members   -  Reputation: 906

Like
0Likes
Like

Posted 11 September 2004 - 10:14 PM

Sure it can be explained a bit further.

TCHAR is actually a typedef. When compiling a unicode build of your project, TCHAR is actually a wchat_t, if the project build is not unicode, then TCHAR resolves to your normal char data type. The difference between wchar_t and char is that wchar_t is 2 bytes in size and it can hold the alphabets from all the languages (biggest one of which is Chinese I think, with around 55,000 characters). char is just used for ASCII characters which can usually only hold english (and other smallers alphabets that use the same symbols.

std::string is the C++ version of a char*. ie: it is used to store string in, and it manages the memory for you. std::wstring is the same thing but uses wchar_t internally instead of char, so you can use it with unicode.

wcs: wide character string
mbs: multi-byte string.

The functions mbstowcs and wcstombs do exactly what they say. one converts from your normal char* to wchar_t* and the other converts from wchar_t* to char*.

So you'll need to use either mbstowcs to wcstombs to convert char to wchar_t and wchar_t to char respectively. Now as if you recall, TCHAR is a wchar_t when UNICODE is defined, and it is a char when UNICODE is not defined. The UNICODE macro is automatically defined by your vc++ compiler if you have the support unicode option set.

So anyway, now Im just going to work with std::String and std::wstring (to use them include the header file "string"). Also keep in mind that to get a C-style character pointer string from an std::string or std::wstring, you need to call their c_str() function:

std::string::c_str();
std::wstring::c_str();

Now on to getting a TCHAR*...

First we define our own type, tstring. tstring is defined as a std::wstring when UNICODE is on and an std::string otherwise. So it's technically the same as a TCHAR*.


tstring ToTString( const std::string& str )
{
}


This function takes in a normal string (ASCII) and converts it to a tstring. Now depending on the UNICODE macro, tstring is either your normal string or your beefed up wstring. So we either return a string or a wstring, depending on if UNICODE is defined on not.

If UNICODE is not defined, we jut return the string that was passed in because tstring is a std::string anyways, but if UNICODE was defined, then we need to convert the std::string that was passed in to a std::wstring, becuase that's what tstring would be. And to do that, we us the mbstowcs conversion function...


tstring ToTString( const std::string& str )
{
#ifdef UNICODE
// Make space for wide string
wchar_t* buffer = new wchar_t[str.size() + 1];

// convert ASCII to UNICODE
mbstowcs( buffer, str.c_str(), str.size() );

// NULL terminate it
buffer[str.size()] = 0;

// Clean memory and return it
std::wstring wstr = buffer;
delete [] buffer;
return wstr;
#else
return str;
#endif
}


#6 tadobie   Members   -  Reputation: 122

Like
0Likes
Like

Posted 11 September 2004 - 10:16 PM

Thanks heaps. That one has been driving me insane!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS