We help IT Professionals succeed at work.

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

_Stilgar_
_Stilgar_ asked
on
2,038 Views
Last Modified: 2013-12-14
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.
Comment
Watch Question

Author

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

Stilgar.
CERTIFIED EXPERT

Commented:
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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());

Author

Commented:
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.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Then I still would go for 'MultiByteToWideChar()'. The flags make it more flexible.

Author

Commented:
Thanks, you have been of a great help
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.