473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

codecvt

Is it possible and ok to use boost::utf8_codecvt_facet to write a
function to convert UTF-16 wchar_t to UTF-8 char and vice versa.

boost::utf8_codecvt_facet
http://www.boost.org/libs/serializat...c/codecvt.html
How to I code the following functions:
string toUTF8(const wstring sUTF16); // converts utf-16 wstring
into utf-8 string
string toUTF16(const string sUTF8); //converts utf-8 string into
utf-16 wstring

thanks

Mar 25 '07 #1
1 3740
On Mar 25, 4:43 pm, inte...@interec.net wrote:
Is it possible and ok to use boost::utf8_codecvt_facet to write a
function to convert UTF-16 wchar_t to UTF-8 char and vice versa.

boost::utf8_codecvt_facethttp://www.boost.org/libs/serialization/doc/codecvt.html

How to I code the following functions:
string toUTF8(const wstring sUTF16); // converts utf-16 wstring
into utf-8 string
string toUTF16(const string sUTF8); //converts utf-8 string into
utf-16 wstring

thanks
I don't know about using the Boost library to do this, but I've
written versions of these functions myself. The trick is to iterate
through the strings one UTF32 character at a time and then re-encode
this in the other format. You *must* go through UTF32 or you'll have
incorrect encodings. You must not encode a UTF16 surrogate pair (for
example) as two UTF-8 sequences.

One way to do this is to write the following bits: iterators that take
a UTF-8 sequence or UTF16 sequence and step through one UTF-32
character at a time. The iterator dereferences to the current UTF-32
character.

Then you want functions for converting a single UTF32 character to
either UTF-8 (up to four characters) or UTF-16 (up to two characters).

With those building blocks it's fairly straightforward to do. I think
you may find codecvt much harder to drive from your own code so unless
somebody has already done it it'll probably be easier to write these
functions yourself.

K

Mar 26 '07 #2

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

Similar topics

2
by: JH Trauntvein | last post by:
I recently learned about the codecvt locale facet by reading Jossutis' (forgive me if I just misspelled his name) book on the C++ standard library (this book has been worth its weight in gold by...
8
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does...
1
by: FabioAng | last post by:
I implemented a codecvt facet (extracted from TCPL) but while it works well with 'ifstream' it doesn't work at all with 'stringstream'. Am I missing anything ? I tested it with MSVC 7.1 The code...
0
by: tictactic | last post by:
Somehow this 'codecvt' class is not part of Watcom C/C++ compiler version 1.5. Can somebody provides a pointer to the implementation of this class?
3
by: Kirit Sælensminde | last post by:
>From thread http://groups.google.com/group/comp.lang.c++/browse_thread/thread/79d767efa42df516 "P.J. Plauger" <p...@dinkumware.comwrites: I'll take this at face value and I'll have to suppose...
10
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is...
1
by: Dancefire | last post by:
Hi, everyone, I'm trying to use std::codecvt<to do the encoding conversion. I am using following code for encoding conversion between wchar_t string and char string(MBCS). I am not sure am I...
6
by: wong_powah | last post by:
How to fix this compile error? # make -f Util/UtilBlock.dep g++ /opt/aCC/include_std/rw/codecvt.cc -o /opt/aCC/include_std/ rw/codecvt /opt/aCC/include_std/rw/codecvt.cc:32: error: use of...
44
by: Ioannis Vranos | last post by:
Has anyone actually managed to print non-English text by using wcout or wprintf and the rest of standard, wide character functions?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.