string conversion
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??
Use the preprocessor:
#ifdef UNICODEtypedef std::wstring tstring;#elsetypedef std::string tstring;#endiftstring 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}
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]
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]
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*.
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...
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}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement