tags:

views:

332

answers:

2
+4  Q: 

MBCS to UTF-8 C++

I'm working on a project in VS2008 that I'm compiling in MBCS but I need to work with some UTF-8 strings to interact with some web services. I wrote a function that works perfectly with Unicode but not MBCS. Is there any way I can convert a MBCS string to UTF-8 or to Unicode?

Thanks!

+8  A: 

Convert the MBCS string to Unicode using MultiByteToWideChar and then to UTF-8 with WideCharToMultiByte. Pass CP_ACP to the first call and CP_UTF8 to the second.

avakar
A good advice in general, but just pay attention that specifying CP_ACP is going to make the behavior dependent on the system language setting of the machine. If that is what he wants it is fine.
Nemanja Trifunovic
When convert MBCS to unicode, you need to have knowledge, on this MBCS is using which code page, right? (BIG5, Simplified Chinese...)
Yan Cheng CHEOK
@Yan Cheng CHEOK, yes. However, most of the time the text will be in the active code page (all the text you retrieve from GUI is in that code page for example).
avakar
A: 

is there a way to do this in C#?

lightyearsaway
well usually C# programmer do is "find a component that does the job for them" :D
YeenFei
I believe MBCS is there for pretty legacy reason, as I do not see MBCS concept in Java anymore. I am not sure about C#. My guess most probably is.
Yan Cheng CHEOK