473,717 Members | 2,223 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert wstring to UTF-8

I'm trying to convert a wstring (actually a BSTR) to UTF-8.

This is what I've currently got:

size_t arraySize;
setlocale(LC_CT YPE,"C-UTF-8");
arraySize = wcstombs(NULL, wstr, 0);
char utf8string[arraySize + 1];
wcstombs(utf8st ring, Name, arraySize + 1);

The problem is that this doesnt seem to convert to UTF-8, it just does
the same as when the setlocale line isnt there.

I've also tried "en_US.UTF-8" instead of "C-UTF-8", but that doesnt
seem to work either.

I'm using Mingw (the version that comes with the latest Dev-C++).

Does anyone know how to make this work?

Sep 8 '06 #1
3 6902

Jared Wiltshire wrote:
I'm trying to convert a wstring (actually a BSTR) to UTF-8.

This is what I've currently got:

size_t arraySize;
setlocale(LC_CT YPE,"C-UTF-8");
arraySize = wcstombs(NULL, wstr, 0);
char utf8string[arraySize + 1];
wcstombs(utf8st ring, Name, arraySize + 1);

The problem is that this doesnt seem to convert to UTF-8, it just does
the same as when the setlocale line isnt there.

I've also tried "en_US.UTF-8" instead of "C-UTF-8", but that doesnt
seem to work either.

I'm using Mingw (the version that comes with the latest Dev-C++).

Does anyone know how to make this work?

The conversion is fairly simple to code yourself. Just remember to
convert to UTF-32 then to UTF-8 and don't convert the individual UTF-16
characters straight to UTF-8. Test it with things like the treble clef
character.
K

Sep 8 '06 #2
Jared Wiltshire schrieb:
I'm trying to convert a wstring (actually a BSTR) to UTF-8.

This is what I've currently got:

size_t arraySize;
setlocale(LC_CT YPE,"C-UTF-8");
arraySize = wcstombs(NULL, wstr, 0);
char utf8string[arraySize + 1];
wcstombs(utf8st ring, Name, arraySize + 1);

The problem is that this doesnt seem to convert to UTF-8, it just does
the same as when the setlocale line isnt there.

I've also tried "en_US.UTF-8" instead of "C-UTF-8", but that doesnt
seem to work either.

I'm using Mingw (the version that comes with the latest Dev-C++).

Does anyone know how to make this work?
This function has already been written, and it's known in some header of
boost (convert.hpp if I'm not wrong). Ok, it may only be a few lines,
but why bother yourself? Additonally, it will work on any compiler
which knows about utf-8 locales.
-- Markus
Sep 8 '06 #3
So no one knows how to get the wcstombs conversion method to work?

I'll have a look at Boost and see how easy it is to copy a conversion
function out of there anyway...

Thanks.

Markus Grueneis wrote:
Jared Wiltshire schrieb:
I'm trying to convert a wstring (actually a BSTR) to UTF-8.

This is what I've currently got:

size_t arraySize;
setlocale(LC_CT YPE,"C-UTF-8");
arraySize = wcstombs(NULL, wstr, 0);
char utf8string[arraySize + 1];
wcstombs(utf8st ring, Name, arraySize + 1);

The problem is that this doesnt seem to convert to UTF-8, it just does
the same as when the setlocale line isnt there.

I've also tried "en_US.UTF-8" instead of "C-UTF-8", but that doesnt
seem to work either.

I'm using Mingw (the version that comes with the latest Dev-C++).

Does anyone know how to make this work?

This function has already been written, and it's known in some header of
boost (convert.hpp if I'm not wrong). Ok, it may only be a few lines,
but why bother yourself? Additonally, it will work on any compiler
which knows about utf-8 locales.
-- Markus
Sep 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
64147
by: gerg | last post by:
How would one convert a wstring to a string? This is what I have so far: bool copyto(std::string& l,std::wstring& r) { bool ret = false; size_t i = 0; const size_t n = r.length()+1;
9
9328
by: ka1cqd | last post by:
I have been looking all over the place for a method to take command line arguments and convert them to a string or wstring so i can process the data and then covert the resulting strings to LPCWSTRs. I have tryed several methods and none work. They include: the convert utilities A2W but that does not compile because of unknow variables declared in the convert header file. MultiByteToWideChar which seems to work once but not the second...
8
16480
by: Divick | last post by:
Hi all, can somebody tell how much std::wstring is supported across different compilers on different platforms? AFAIK std::string is supported by almost all C++ compilers and almost all platforms, is that also the case with wstring? Another related question that I have is, is it advisable to use wstring than string for unicode support? To be able to support Unicode build, is it that all the occurrence of std::string will need to be...
14
12192
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++ books (Effective C++, More Effective C++, Exceptional C++, More Exceptional C++, C++ FAQs, Addison Wesley 2nd Edition) Authors of these books have not considered UNICODE. So many of their
4
17779
by: thinktwice | last post by:
i'm using VC++6 IDE i know i could use macros like A2T, T2A, but is there any way more decent way to do this?
10
4861
by: v4vijayakumar | last post by:
1. why the following program is not working as expected? #include <iostream> using namespace std; int main() { string t("test"); wcout << (wchar_t *) t.c_str() << endl; wcout << t.c_str() << endl;
1
8228
by: ycinar | last post by:
How can I convert a string (or a wstring) to a PWCHAR? PWCHAR temp; wstring wstr; // Basically I want to do // temp = wstr // but casting didnt work // temp = PWCHAR (wstr) // error C2440: 'type cast' : cannot convert from 'std::wstring' to 'PWCHAR'
6
4204
by: Jeffrey Walton | last post by:
Hi All, I'm attempting to write a wstring to a file by way of wofstream. I'm getting compression on the stream (I presume it is UTF-8). How/where do I invoke an alternate constructotor so that the stream stays wide (UTF-16)? I suspect that it is hidden in a locale, but I don't have much experience with them. I also have not been able to locate it in Stroustrup: Appendix D: Locales . does state the following, but
4
6870
by: =?ISO-8859-2?Q?Boris_Du=B9ek?= | last post by:
Hi, I have an API that returns UTF-8 encoded strings. I have a utf8 codevt facet available to do the conversion from UTF-8 to wchar_t encoding defined by the platform. I have no trouble converting when a UTF-8 encoded string comes from file - I just create a std::wifstream and imbue it with a locale that uses the utf-8 facet for std::locale::ctype. Then I just use operator>to get wstring properly decoded from UTF-8. I thought I could...
0
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9105
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6647
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5969
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2543
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2119
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.