473,386 Members | 1,652 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,386 software developers and data experts.

c++ support for unicode, utf-8, encode/decode, ifstream, wstream?


Hi,
I have an UNICODE text file endcoded in UTF-8.

I should store the UNICODE strings in my program for example in
std::wstring right? To be able to work on them normally, so that
std::wstring foo; foo[5] would mean 5-th _character_, and not 5-th
byte of UNICODE encoded string.

How do I read a text from UTF-8 file into std::wstring? I need to do
some conversion right? from utf-8 to internal format used by
std::wstring (probably UCS-2 or -4 right?)

Also, how to save back the string, and how to manipulate it (like,
replace 4-th character, just str[4]=(wchar)'x' ?)

Thanks



Jan 20 '06 #1
12 9847
TB
Rafał Maj Raf256 sade:
Hi,
I have an UNICODE text file endcoded in UTF-8.

I should store the UNICODE strings in my program for example in
std::wstring right? To be able to work on them normally, so that
std::wstring foo; foo[5] would mean 5-th _character_, and not 5-th
byte of UNICODE encoded string.

How do I read a text from UTF-8 file into std::wstring? I need to do
some conversion right? from utf-8 to internal format used by
std::wstring (probably UCS-2 or -4 right?)

Also, how to save back the string, and how to manipulate it (like,
replace 4-th character, just str[4]=(wchar)'x' ?)


Upon reading the UTF-8 data convert it internally to UTF-32 for
easier parsing. The conversion process is quite easy to write.
The problem with std::wstring is that it's templatized with
wchar_t, and that primitive is at least on my machine only 2 bytes,
and therefore not practical to use with unicode (unless you actually
wish to use the abnormal UTF-16 variant in such a case).

--
TB @ SWEDEN
Jan 20 '06 #2
TB wrote:
Upon reading the UTF-8 data convert it internally to UTF-32 for
easier parsing.
How? Arent there ready to use functions/classes doing that? In std,
perhaps in boost?
The conversion process is quite easy to write. The problem with std::wstring is that it's templatized with
wchar_t, and that primitive is at least on my machine only 2 bytes,
and therefore not practical to use with unicode (unless you actually
wish to use the abnormal UTF-16 variant in such a case).


Hm.. so which class is best to store any-language text string then?

Jan 20 '06 #3
"Rafal Maj Raf256" <us*******************@raf256.com.invalid> wrote in
message news:dq**********@inews.gazeta.pl...
TB wrote:
Upon reading the UTF-8 data convert it internally to UTF-32 for
easier parsing.


How? Arent there ready to use functions/classes doing that? In std,
perhaps in boost?


You'll find a few codecvt facets (the critters you need) in various places,
but for a complete set of all that you're likely to need -- ready made,
tested, and supported -- see our CoreX library.
The conversion process is quite easy to write.
No it isn't. At least not correctly and robustly.
The problem with std::wstring is that it's templatized with
wchar_t, and that primitive is at least on my machine only 2 bytes,
and therefore not practical to use with unicode (unless you actually
wish to use the abnormal UTF-16 variant in such a case).


Hm.. so which class is best to store any-language text string then?


Depends on your goals. In truth and reality, you can still get away quite
nicely with UCS-2. Effectively, you ignore the exotic characters with
code values above 0xffff more recently added. Your input converter
then treats as erroneous any UTF-8 sequence that specifies a code
value that's too big. But if you feel the need to support the complete
Unicode set in its current form, you need to convert UTF-8 to UTF-16
internally, and accept the fact that characters can occupy either one or
two storage elements. Whatever your choice, CoreX has the
conversion tools you need to carry it out.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jan 20 '06 #4
TB
Rafał Maj Raf256 sade:
TB wrote:
Upon reading the UTF-8 data convert it internally to UTF-32 for
easier parsing.


How? Arent there ready to use functions/classes doing that? In std,
perhaps in boost?
The conversion process is quite easy to write.

The problem with std::wstring is that it's templatized with
wchar_t, and that primitive is at least on my machine only 2 bytes,
and therefore not practical to use with unicode (unless you actually
wish to use the abnormal UTF-16 variant in such a case).


Hm.. so which class is best to store any-language text string then?


If 'unsigned int' is 4 bytes on your machine, write a unicode
implementation based on that primitive, or use an already available
framework; hm, perhaps this 'CoreX'-thingy advocated by P.J.

--
TB @ SWEDEN
Jan 20 '06 #5
"TB" <TB@void.com> wrote in message
news:43**********************@taz.nntpserver.com.. .
Rafal Maj Raf256 sade:
TB wrote:
Upon reading the UTF-8 data convert it internally to UTF-32 for
easier parsing.


How? Arent there ready to use functions/classes doing that? In std,
perhaps in boost?
The conversion process is quite easy to write.

The problem with std::wstring is that it's templatized with
wchar_t, and that primitive is at least on my machine only 2 bytes,
and therefore not practical to use with unicode (unless you actually
wish to use the abnormal UTF-16 variant in such a case).


Hm.. so which class is best to store any-language text string then?


If 'unsigned int' is 4 bytes on your machine, write a unicode
implementation based on that primitive, or use an already available
framework; hm, perhaps this 'CoreX'-thingy advocated by P.J.


Yep. It includes UTF-8 to UCS-4 too. And it's templatized on the
internal character type. Forgot to mention that.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jan 20 '06 #6
On Fri, 20 Jan 2006 17:13:37 +0100, TB <TB@void.com> wrote:
If 'unsigned int' is 4 bytes on your machine, write a unicode
implementation based on that primitive, or use an already available
framework; hm, perhaps this 'CoreX'-thingy advocated by P.J.


Oh, it isn't just advocated by Mr. Plauger it's SOLD ($) by Mr.
Plauger. Bit of a difference I think.

"If you have ten thousand regulations you destroy
all respect for the law." - Winston Churchill
Jan 21 '06 #7
"JustBoo" <Ju*****@BooWho.com> wrote in message
news:e5********************************@4ax.com...
On Fri, 20 Jan 2006 17:13:37 +0100, TB <TB@void.com> wrote:
If 'unsigned int' is 4 bytes on your machine, write a unicode
implementation based on that primitive, or use an already available
framework; hm, perhaps this 'CoreX'-thingy advocated by P.J.


Oh, it isn't just advocated by Mr. Plauger it's SOLD ($) by Mr.
Plauger. Bit of a difference I think.


Really? In what way? I certainly *advocate* using an already
available framework, as did TB. If you can get a free one that
does the job (and it's still sufficiently "free" after you locate
it, download it, figure out how to build it, integrate it into
your product, deal with the surprises, and test it to your
satisfaction) by all means do so. I also *advocate* using CoreX,
if you're sufficiently professional that USD 90 is cheaper than
the above parenthetical exercise costs you in your time and
peace of mind.

But if you think I *advocate* something just because I make
ninety bucks off it, then by all means avoid anything that's
$OLD and stick with open sour¢e. Just don't measure me by
your standards.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jan 21 '06 #8
On Sat, 21 Jan 2006 18:32:22 -0500, "P.J. Plauger"
<pj*@dinkumware.com> wrote:
"JustBoo" <Ju*****@BooWho.com> wrote in message
news:e5********************************@4ax.com.. .
Oh, it isn't just advocated by Mr. Plauger it's SOLD ($) by Mr.
Plauger. Bit of a difference I think.
Really? In what way? I certainly *advocate* using an already
available framework, as did TB.
In what way? Well, from the simple *fact* you make money selling
your products here. I make that the point of this paragraph. Can you
deny that? It is a fact. Leave emotion out of it. Leave capitalism out
of it. Leave your perception that this is an insult out of it, and all
the rest. You sell your products here. I could truly *careless*
whether you do or not. But you do. Please do not enumerate all the
good you do for the free and not-so-free world by doing this. You sell
them here on a consistent basis. Period. As the sun comes up every
morning, it's just the obvious truth. Now please pay attention; in the
*context* of this thread, I thought it important to point this out to
the poster. That's it.

Before getting your hackles up please read further.
If you can get a free one that
does the job (and it's still sufficiently "free" after you locate
it, download it, figure out how to build it, integrate it into
your product, deal with the surprises, and test it to your
satisfaction) by all means do so. I also *advocate* using CoreX,
if you're sufficiently professional that USD 90 is cheaper than
the above parenthetical exercise costs you in your time and
peace of mind.
Fairly ironic that. I'm certain you don't remember, but *I have
recommended* people look at your products on a regular basis, in
this ng and others. Even in the real world. And ready for this, I have
used precisely the exact same logic to justify the recommendation
when *attacked* for doing so. That is the very definition of irony.

[Note: I usually leave out the snarky remark about "sufficiently
professional" though.] :-)
But if you think I *advocate* something just because I make
ninety bucks off it, then by all means avoid anything that's
$OLD and stick with open sour¢e.
Wow, an ocean's worth of assumption and presumptions to boot. You
think me a socialist? Bwha. I'm a stone-cold capitalist. You've
assumed far too much. <chuckling> You've read far too much into my
simple statement of fact.

And yes, I do think you advocate it because you make money from it.
Welcome to the commerce of the human race. It's just human nature.
I'll leave it up to you to decide if that is an insult or not.

Trend your own posts, seriously. Look at what you respond to and what
you always recommend. I believe you to be of a scientific mentality
and if you are honest with yourself you will see truth. Noting more
nothing less.

And in the end, so what. As I'm sure one of your arguments would/will
be: people are free to buy it or not, and you're making them aware of
its existence. And there you have it. Try to read this post without
emotion and perhaps you'll see my intent.
Just don't measure me by
your standards.

[Insult acknowledged but not accepted; like a refused package]

Once again, you assume far too much. Especially given that I simply
pointed out that you sell products, which is true. Does being a
capitalist bother you? Guilt perhaps? Note those are questions, not
assumptions.

"I didn't fight my way to the top of the food chain to be a
vegetarian."

Have a *prosperous* week. :-)
Jan 22 '06 #9
"JustBoo" <Ju*****@BooWho.com> wrote in message
news:h6********************************@4ax.com...
On Sat, 21 Jan 2006 18:32:22 -0500, "P.J. Plauger"
<pj*@dinkumware.com> wrote:
"JustBoo" <Ju*****@BooWho.com> wrote in message
news:e5********************************@4ax.com. ..
Oh, it isn't just advocated by Mr. Plauger it's SOLD ($) by Mr.
Plauger. Bit of a difference I think.
Really? In what way? I certainly *advocate* using an already
available framework, as did TB.


In what way? Well, from the simple *fact* you make money selling
your products here. I make that the point of this paragraph. Can you
deny that?


Uh, no.
[extensive rant elided]


Got it. Now chill out.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jan 22 '06 #10
On Sun, 22 Jan 2006 12:13:04 -0500, "P.J. Plauger"
<pj*@dinkumware.com> wrote:
[extensive rant elided]


Got it. Now chill out.


Extensive rant? Wow. obviously the "voice" I wrote the post with and
the "voice" you read it with were in two different universes.

I said repeatedly "without emotion." To me that means even and calm.
But...

Boo to airline stewardess on Plauger Air,
"Miss, may I have a pillow?"

Stewardess: "AHHHH! AHHH, STOP SCREAMING
AT ME YOU ANGRY, BITTER, MEAN, ANGRY,
UNREASONABLE COMMIE, SOCIALIST,
BASTARD! AHHHHH, AHHHHHH!."

<sigh> All righty then....

Men occasionally stumble over the truth, but most
of them pick themselves up and hurry off as if
nothing had happened. - Winston Churchill

Once again, Have a *prosperous* week.
I'm out for some fun in the sun. :-)
Jan 22 '06 #11
"JustBoo" <Ju*****@BooWho.com> wrote in message
news:o5********************************@4ax.com...
On Sun, 22 Jan 2006 12:13:04 -0500, "P.J. Plauger"
<pj*@dinkumware.com> wrote:
[extensive rant elided]


Got it. Now chill out.


Extensive rant? Wow. obviously the "voice" I wrote the post with and
the "voice" you read it with were in two different universes.

I said repeatedly "without emotion." To me that means even and calm.
But...


Yes, you said many things, but it's clear that you are
intellectually dishonest. I stay in "dialogs" like this
only when I feel a need to educate the lurkers, not in
any hopes of finding common ground. Since I have no
sermon to preach in this case, I see little reason to
feed this particular troll.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jan 22 '06 #12
On Sun, 22 Jan 2006 14:11:07 -0500, "P.J. Plauger"
<pj*@dinkumware.com> wrote:
"JustBoo" <Ju*****@BooWho.com> wrote in message
news:o5********************************@4ax.com.. .
Extensive rant? Wow. obviously the "voice" I wrote the post with and
the "voice" you read it with were in two different universes.
Well, I now "see" what voice you hear. It seems obvious to me you have
quite literally no sense-of-humor. I mean you literally have no
ability to sense when humor is being applied to a situation. I now
have no doubt when someone makes a witty/funny remark in a meeting
you're that guy that sits there stone-faced because you just don't get
it.

If I make a "thumbs-up" gesture and say how many fingers am I holding
up you're the guy that has to launch into a deadly-dull bone-dry
dissertation about why thumbs aren't fingers and blah, blah... yawn,
blah.... Wow.

The airline joke (or an attempt at one :-) ) was so over the top that
I figured *anyone* with a social IQ above a warm doorknob would
get it. Disappointed again I see. And yes, that is an insult. YOU
insulted me numerous times in the past posts and I did *not*
reciprocate. I left them unanswered. Think about that.

"I like a man who grins when he fights." - Winston Churchill
I said repeatedly "without emotion." To me that means even and calm.
But...


Yes, you said many things, but it's clear that you are
intellectually dishonest. I stay in "dialogs" like this
only when I feel a need to educate the lurkers, not in
any hopes of finding common ground.


Gah! Where the heck did that come from? Oh, right, no ability to sense
humor. But there seems to be more... hmm, let's see. Admitted to
hawking wares, okay, but he feels "a need to educate the lurkers."
Hmm, he knows he has good products so should not be so insecure that
he has to defend that at every turn and that was not even brought up
in this thread, (but I am probably wrong about that...) but… oh no!
Say isn't so. :-(

Well, I think he has decided he is the Alpha Male of this (his?)
newsgroup and needs to prove to his underlings, lackeys and lurkers
that he will have the ego-driven Last Word(tm)(r), by gawd! Yes, it's
Good To Be King.

Man, that explains so much of the behavior, tone and tenor of the
petty back-biting Superior Sheep in this group. The lesser
monkey-sheep are emulating The Alpha Males behavior. Much of it to
curry favor and not be "chewed on" by the emulating monkey-sheep.
Ding! Because after all, they are so superior to the sub-creatures
that will not submit.

Man, what a small tiny vacuous life to live. To each his own.

"To perceive is to suffer." - Aristotle
Since I have no
sermon to preach in this case, I see little reason to
feed this particular troll.


And you call me *intellectually* dishonest? Bwha. Look in the mirror,
pal.

Life should NOT be a journey to the grave with the intention of
arriving safely in a well preserved body, but to skid in sideways,
champagne in one hand, chocolate in the other, body thoroughly
used up and worn out, screaming "WOO HOO BOO- What a Ride!"
- Terry Pratchett
Jan 23 '06 #13

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

Similar topics

4
by: Aditya Ivaturi | last post by:
We have a CMS which is written is based on php & mysql. Recently we received a request to support multiple languages so that sites in that particular laguage can be created. I did some search on...
4
by: gabor | last post by:
hi, today i made some tests... i tested some unicode symbols, that are above the 16bit limit (gothic:http://www.unicode.org/charts/PDF/U10330.pdf) .. i played around with iconv and so on,...
1
by: krammer | last post by:
Hello, I have the following questions that I have not been able to find any *good* answers for. Your help would me much appreciated!, fyi, I am a Java XML guy and I have no experience with SGML...
3
by: Kieran Green | last post by:
Greetings, We are building an application written for Windows in C++ which uses OLEDB to connect to AIX DB2 8.2. Our app stores all string data in the wchar_t datatype, which generates dynamic...
11
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The program listed below demonstrates the use of wcsftime() and std::time_put<wchar_t> which is a C++ wrapper around it. (I know this isn't C; but...
2
by: hezhenjie | last post by:
Hi, all: I just need to parse a unicode file, and assume to get data one line by one line. I use _wfopen(), fgetws(), wcslen(), wcsstr(), making it work normally on Windows platform. However,...
8
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,...
8
by: sonald | last post by:
Hi, I am using python2.4.1 I need to pass russian text into python and validate the same. Can u plz guide me on how to make my existing code support the russian text. Is there any module...
18
by: Chameleon | last post by:
I am trying to #define this: #ifdef UNICODE_STRINGS #define UC16 L typedef wstring String; #else #define UC16 typedef string String; #endif ....
3
by: =?Utf-8?B?QWxleGFuZGVy?= | last post by:
Hi! I don't know why, but I want to read a file, change some of the content, and want to write this new content in another file. The problem is, that it contains unicode text. My code is: ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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.