[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-10-05
8
Medium Priority
?
1,803 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.
0
Comment
Question by:_Stilgar_
  • 4
  • 3
8 Comments
 
LVL 14

Author Comment

by:_Stilgar_
ID: 20022310
If that matters, I'm using VC2005, no MFC.

Stilgar.
0
 
LVL 20

Expert Comment

by:ikework
ID: 20022646
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 20023000
>> was wondering what exactly is the difference between that and mbstowcs?

'MultiByteToWideChar()' allows you to specify a codepage, whereas 'mbstowcs()' uses the codepage specified in the locale.

>>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?

That won't make a big difference performance-wise. However, if you want to save memory, I'd rather keep only one storage (the UNICODE version preferably) and thus doing the conversion when the data arrives.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Author Comment

by:_Stilgar_
ID: 20028285
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20028364
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());
0
 
LVL 14

Author Comment

by:_Stilgar_
ID: 20028389
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20028402
Then I still would go for 'MultiByteToWideChar()'. The flags make it more flexible.
0
 
LVL 14

Author Comment

by:_Stilgar_
ID: 20028431
Thanks, you have been of a great help
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question