Wide String to Normal
How would you go about turning a wchar_t into a lptstr ?
[56 byte] By [
SuperCode] at [2007-11-17 22:38:54]

# 2 Re: Wide String to Normal
Yes, check in the FAQ ( http://www.dev-archive.com/FAQS/) There are a few examples.
# 3 Re: Wide String to Normal
Inspecting the original question with my eyes (something I do not always do before I answer), I see that it is asking for a conversion from a wchar_t (not a pointer?) to lptstr which is quite a different can of worms. Since the t is dependent upon a preprocessor define, I would make a preprocessor wrap around the conversion.
#ifdef UNICODE
// define a converter here for just taking the wchar_t and making a zero
//delimited unicode string from the one character
#else
// define the converter to make the conversion on one char
//(in the appropriate param) with WideCharToMultiByte
#endif
I would normally answer this question as the previous two posts, but I think sometimes I assume too much, so I though the original poster might like a start in this direction. Of course, I didn't go into the actual definition, because I was a little confused about whether the intent was really wchar_t or wchar_t* (and thus whether to go to all the trouble of address referencing and possible zero-termination appending), but those details can easily be filled in!
[Yves : please avoid long code lines ;)]
# 4 Re: Wide String to Normal
If you're trying to convert wchar_t string to single byte char string, then you should use wcstombs function.
WideCharToMultibyte is not part of the C++ standard, and therefore not portable.
wcstombs is part of the C and C++ standard.
Here's some example code:
#include <stdlib.h>
int main(int, char**)
{
wchar_t *wp = L"This is a test";
char Dest_c[50];
wcstombs(Dest_c, wp, sizeof(Dest_c));
printf("Hello World!\n%s\n", Dest_c);
system("pause");
return 0;
}
Axter at 2007-11-8 1:14:20 >

# 5 Re: Wide String to Normal
wchar_t s1[30]=L"abcd";
char s2[30]="";
sprintf(s2, "%S", s1);
printf("\n%S\n%s", s1, s2);