Greenhorn
Greenhorn

Reputation: 658

How can I convert a wchar_t* to char* without losing data?

I'm using a Japanese string as a wchar_t, and I need to convert it to a char*. Is there any method or function to convert wchar_t* to char* without losing data?

Upvotes: 2

Views: 1486

Answers (3)

bmargulies
bmargulies

Reputation: 99993

You need to call WideCharToMultiByte and pass in the code page encoding identifier for the Japanese multibyte encoding you want. See the MDSN for that function. On Windows, the local multibyte set is CP932, the MS variation on ShiftJIS. However, you might conceivably want UTF-8 to send to someone who wants it.

Upvotes: 2

Jon
Jon

Reputation: 437326

It is not enough to say "I have a string as wchar_t". You must also know what encoding the characters of the string are in. This is probably UTF-16, but you need to know definitely.

It is also not enough to say "I want to convert to char". Again, you must make a decision on what encoding the characters will be represented in. JIS? Shift-JIS? EUC? UTF-8? Another encoding?

If you know the answers to the two questions above, you can do the conversion without any problem using WideCharToMultiByte.

Upvotes: 8

minhee
minhee

Reputation: 5818

What you have to do first is to choose the string encoding such as UTF-8 or UTF-16. And then, encode your wchar_t[] strings in the encoding you choose via libiconv or other similar string encoding library.

Upvotes: 2

Related Questions