2

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?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Greenhorn
  • 658
  • 2
  • 8
  • 24

3 Answers3

8

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
2

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.

minhee
  • 5,688
  • 5
  • 43
  • 81
2

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.

bmargulies
  • 97,814
  • 39
  • 186
  • 310