What's new

WCHAR to char

Doomulation

?????????????????????????
I've been thinking about this forever now... <_<
It's fekking sneaky that directmusic requires wchar and not char.
I can't find a way to convert char to wchar.
VB also passes strings as char* so putting an argument of wchar* doesn't work. I also tried sending a integer array since wchar is unsigned short, but it didn't work either.

strcat cannot copy a string (char) to wchar correctly either.

A little help on how to do this, please?
 

N-Rage

New member
wchar is used for Unicode encoding.
The problem with ACSII and other 8Bit Character encoding is that you can easily run into trouble if encodings are mixed.
Also languages like Chinese need more than 256 Characters... thats the reason Unicode came up, it contains any character of any language-typeset.

Anyway, in the Windows-API are 2 functions to convert between Unicode and 8bit-encodings.

Those are called MultiByteToWideChar and WideCharToMultiByte.

Also in C/C++ you get Unicode strings if you put a L before the quotes i.e.: L"Widechar string", works only for literals though.
 
Last edited:

Cyberman

Moderator
Moderator
NRage.. NO! bad Rage.. Those are not going to work. Multibyte is not the same thing as char or wchar it's a completely different animal. It's compressed unicode and the encoding is very different.

Unfortunately I've been playing with unicode because PSX cards are enocded using SJIS format (damn MS formats). So I've experienced converting.

wide char to char is not easy to do sometimes however the simplest thing is doing this.

Code:
void strcpy_wc(char *Dest, wchar_t *Src)
{
   while (*Src)
   {
      *Dest++ = *Src++;
   }
   *Dest = 0;
}
void strcpy_cw(wchar_t *Dest, char *Src)
{
   while (*Src)
   {
      *Dest++ = *Src++;
   }
   *Dest = 0;
}

Believe it or not this works with ASCII characters quite nicely. They must be ASCII though, do not try this with anything other than ASCII encoded data it won't work to hot. It will not work essentially. Also you must be passing ARRAYS of wchar_t type to the functions you cannot pass windows STRINGS they are a completely different animal.

You should see what you have to do with clipboard data (eye yi yi!).

Cyb
 

N-Rage

New member
MultiByteToWideChar and WideCharToMultiByte are, believe it or not, able to take also ANSI and other 8bit Codepages.

So, they can do exactly the same as your functions, but they arent limited to ANSI( which just works in your function cause the first 256 Characters of Unicode match with the ANSI Standard ). Just thing you would use your function with a Codepage OTHER than ANSI, the result would be garbage.

the safest way to convert a string to unicode is this:

char szSource[] = "Text to convert";
wchar szDest[100];
MultiByteToWideChar( GetACP(), 0, szSource, -1, szDest, sizeof(szDest)/sizeof(szDest[0]) );
 
Last edited:
OP
Doomulation

Doomulation

?????????????????????????
Thanks for the help, but i already figured it out by studing sample dx apps :blush:
N-Rage is right. There is "helper" dx functions which used those apis you specified, n-rage. It all works great now.
 

Cyberman

Moderator
Moderator
Well maybe I was dealing with SJIS data.. hmm likely the case :)
anyhow the two functions I show there work with SJIS convereted to Unicode at least. Actually that's what I used for clipboard data. Hmmm seems the clipboard for 95 doesn't support unicode too well :)

Cyb
 

tooie

New member
95 does not support unicode well .. for that matter the 9x family doesnt most of the W function just return an error of not being implemented.

N-Rage is right you should use MultiByteToWideChar and WideCharToMultiByte
 

Top