Link to home
Start Free TrialLog in
Avatar of _Stilgar_
_Stilgar_Flag for Israel

asked on

BYTE*/char* to UTF8 using mbstowcs or MultiByteToWideChar?

Hi,

In a function downloading from the internet I get pieces of the data as char*/BYTE* (doesn't really matter...).

Since this will be used for downloading HTML/XML files too, I need to port convert it to UTF8. I always used MultiByteToWideChar, and I was wondering what exactly is the difference between that and mbstowcs?

Also, what is better - to use that conversion with each piece (65536 buffer), or to pile them up in std::string or whatever and then port it at once to wstring?

Thanks,

Stilgar.
Avatar of _Stilgar_
_Stilgar_
Flag of Israel image

ASKER

If that matters, I'm using VC2005, no MFC.

Stilgar.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Since MultiByteToWideChar needs a buffer to make the conversion, I will need to create a new buffer for each piece in order to convert it and append it to the wstring. That means many new memory allocations and deletes against many memory re-allocations (when adding this to std::string during the actual download for later conversion). What would be your go?

Stilgar.
No, you can reuse the buffer, since assingning it to a wstring copies the contents. BTW, there is also another way wor such a conversion, i.e.


#include <ctype>
#include <string>
using namespace std;

string str = "test";

wstring wstr = widen(str.begin(),str.end());
I get Cannot open include file: 'ctype': No such file or directory, and can't find widen() anywhere.

Anyway, does it work as MultiByteToWideChar(), and doesn't use the locale codepage? I need to convert to UTF8.

Stilgar.
Then I still would go for 'MultiByteToWideChar()'. The flags make it more flexible.
Thanks, you have been of a great help