473,853 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unicode I/O

Hi,

The following std c++ program does not output the unicode
character.:-

%./a.out
en_US.UTF-8
Infinity:
%cat unicode.cpp
#include<iostre am>
#include<string >
#include<locale >

int main()
{
std::wstring ws = L"Infinity: \u221E";
std::locale loc("");
std::cout << loc.name( ) << " " << std::endl;
std::wcout.imbu e(loc);
std::wcout << ws << std::endl;
}
%locale
LANG=en_US
LC_CTYPE="en_US .UTF-8"
LC_NUMERIC="en_ US.UTF-8"
LC_TIME="en_US. UTF-8"
LC_COLLATE="en_ US.UTF-8"
LC_MONETARY="en _US.UTF-8"
LC_MESSAGES="en _US.UTF-8"
LC_PAPER="en_US .UTF-8"
LC_NAME="en_US. UTF-8"
LC_ADDRESS="en_ US.UTF-8"
LC_TELEPHONE="e n_US.UTF-8"
LC_MEASUREMENT= "en_US.UTF-8"
LC_IDENTIFICATI ON="en_US.UTF-8"
LC_ALL=en_US.UT F-8

What am I missing? Kindly help. I am trying to understand unicode i/o
in c++ to modify an application to work for unicode input.

Thank You,
Himanshu
Jun 27 '08 #1
10 3284
hi***********@g mail.com wrote:
Hi,

The following std c++ program does not output the unicode
character.:-

%./a.out
en_US.UTF-8
Infinity:
%cat unicode.cpp
#include<iostre am>
#include<string >
#include<locale >

int main()
{
std::wstring ws = L"Infinity: \u221E";
std::locale loc("");
std::cout << loc.name( ) << " " << std::endl;
std::wcout.imbu e(loc);
std::wcout << ws << std::endl;
}

Unicode support is not included by current C++ standard,
In C++0x, there will be Unicode support.

I'm quite dummy about locale things,
But you can check Boost utf8_codecvt_fa cet, which works very well.

I'm sure there's no "\u" escape character in C++,
while Java has this.

HTH.

--
Best Regards
Barry

Jun 27 '08 #2
On 13 avr, 10:58, Barry <dhb2...@gmail. comwrote:
himanshu.g...@g mail.com wrote:
The following std c++ program does not output the unicode
character.:-
%./a.out
en_US.UTF-8
Infinity:
%cat unicode.cpp
#include<iostre am>
#include<string >
#include<locale >
int main()
{
std::wstring ws = L"Infinity: \u221E";
std::locale loc("");
std::cout << loc.name( ) << " " << std::endl;
std::wcout.imbu e(loc);
std::wcout << ws << std::endl;
}
Unicode support is not included by current C++ standard,
Full Unicode support isn't there, but there are a few things.
L"\u221E", for example, is guaranteed to be the infinity sign in
an implementation defined default wide character encoding,
supposing it exists. And Posix (not C++) guarantees that the
locale "en_US.UTF-8" uses UTF-8 encoding. So at the very least,
from a quality of implementation point of view, if nothing else,
he should either get a warning from the compiler (that the
character requested character isn't available), throw
std::runtime_er ror to indicate that the requested locale isn't
supported, or the character he wants, correctly encoded in
UTF-8. (Technically, the behavior of locale("") is
implementation defined, and I don't think it's allowed to raise
an exception. But in this case, an implementation under a
system using the Posix locale naming conventions shouldn't
return "en_US.UTF-8" as the name, but rather something like
"C".)

What I would do in his case, for starters, is do a hex dump of
the wstring's buffer, to see exactly how L"\u221E" is encoded.
Beyond that: if it's encoded as some default character indicated
a non-supported character, then he should file an error report
with the compiler, requesting a warning, otherwise, he should
file an error report for the library, indicating that locales
aren't working as specified.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3
On 13 Apr., 08:49, himanshu.g...@g mail.com wrote:
Hi,

* * The following std c++ program does not output the unicode
character.:-

%./a.out
en_US.UTF-8
Infinity:

%cat unicode.cpp
#include<iostre am>
#include<string >
#include<locale >

int main()
{
* *std::wstring ws = L"Infinity: \u221E";
* *std::locale loc("");
* *std::cout << loc.name( ) << " " << std::endl;
* *std::wcout.imb ue(loc);
* *std::wcout << ws << std::endl;

}
[snip]
>
What am I missing? Kindly help. I am trying to understand unicode i/o
in c++ to modify an application to work for unicode input.
I don't know the solution to your problem, but first of all, I'd write
to a file first, and not to the console. Examine the file - preferably
with a hex editor - and verify if it is in the proper format. If it
is, the C++ part might be okay, and you should look for a solution
somewhere else.

/Peter
Jun 27 '08 #4
On Apr 13, 5:59*pm, James Kanze <james.ka...@gm ail.comwrote:
On 13 avr, 10:58, Barry <dhb2...@gmail. comwrote:
himanshu.g...@g mail.com wrote:
The following std c++ program does not output the unicode
character.:-
%./a.out
en_US.UTF-8
Infinity:
%cat unicode.cpp
#include<iostre am>
#include<string >
#include<locale >
int main()
{
* *std::wstring ws = L"Infinity: \u221E";
* *std::locale loc("");
* *std::cout << loc.name( ) << " " << std::endl;
* *std::wcout.imb ue(loc);
* *std::wcout << ws << std::endl;
}
Unicode support is not included by current C++ standard,

Full Unicode support isn't there, but there are a few things.
L"\u221E", for example, is guaranteed to be the infinity sign in
an implementation defined default wide character encoding,
supposing it exists. *And Posix (not C++) guarantees that the
locale "en_US.UTF-8" uses UTF-8 encoding. *So at the very least,
from a quality of implementation point of view, if nothing else,
he should either get a warning from the compiler (that the
character requested character isn't available), throw
std::runtime_er ror to indicate that the requested locale isn't
supported, or the character he wants, correctly encoded in
UTF-8. *(Technically, the behavior of locale("") is
implementation defined, and I don't think it's allowed to raise
an exception. *But in this case, an implementation under a
system using the Posix locale naming conventions shouldn't
return "en_US.UTF-8" as the name, but rather something like
"C".)

What I would do in his case, for starters, is do a hex dump of
the wstring's buffer, to see exactly how L"\u221E" is encoded.
Beyond that: if it's encoded as some default character indicated
a non-supported character, then he should file an error report
with the compiler, requesting a warning, otherwise, he should
file an error report for the library, indicating that locales
aren't working as specified.
James, thanks for correcting me.

I review the standard about \u and \U.
Now I'm *sure* that my assertion about the "\u" was wrong.

I run the code, realize that (Platform : Windows XP, VC8)

dumping L"\u4e00" become "0x4e 0xA1" which is exactly UTF-16,
the default Unicode transformation on Windows.

dumping "\u4e00" become "0xB6 0xA1" which is GBK encoding (mbcs),
my default encoding setting.

Is it this conversion done directly by the compiler?

Jun 27 '08 #5
On Apr 13, 8:36*pm, Barry <dhb2...@gmail. comwrote:
On Apr 13, 5:59*pm, James Kanze <james.ka...@gm ail.comwrote:
On 13 avr, 10:58, Barry <dhb2...@gmail. comwrote:
himanshu.g...@g mail.com wrote:
The following std c++ program does not output the unicode
character.:-
%./a.out
en_US.UTF-8
Infinity:
%cat unicode.cpp
#include<iostre am>
#include<string >
#include<locale >
int main()
{
* *std::wstring ws = L"Infinity: \u221E";
* *std::locale loc("");
* *std::cout << loc.name( ) << " " << std::endl;
* *std::wcout.imb ue(loc);
* *std::wcout << ws << std::endl;
}
Unicode support is not included by current C++ standard,
Full Unicode support isn't there, but there are a few things.
L"\u221E", for example, is guaranteed to be the infinity sign in
an implementation defined default wide character encoding,
supposing it exists. *And Posix (not C++) guarantees that the
locale "en_US.UTF-8" uses UTF-8 encoding. *So at the very least,
from a quality of implementation point of view, if nothing else,
he should either get a warning from the compiler (that the
character requested character isn't available), throw
std::runtime_er ror to indicate that the requested locale isn't
supported, or the character he wants, correctly encoded in
UTF-8. *(Technically, the behavior of locale("") is
implementation defined, and I don't think it's allowed to raise
an exception. *But in this case, an implementation under a
system using the Posix locale naming conventions shouldn't
return "en_US.UTF-8" as the name, but rather something like
"C".)
What I would do in his case, for starters, is do a hex dump of
the wstring's buffer, to see exactly how L"\u221E" is encoded.
Beyond that: if it's encoded as some default character indicated
a non-supported character, then he should file an error report
with the compiler, requesting a warning, otherwise, he should
file an error report for the library, indicating that locales
aren't working as specified.

James, thanks for correcting me.

I review the standard about \u and \U.
Now I'm *sure* that my assertion about the "\u" was wrong.

I run the code, realize that (Platform : Windows XP, VC8)

dumping L"\u4e00" become "0x4e 0xA1" which is exactly UTF-16,
sorry 0x00

Jun 27 '08 #6
On 13 avr, 14:36, Barry <dhb2...@gmail. comwrote:
On Apr 13, 5:59 pm, James Kanze <james.ka...@gm ail.comwrote:
On 13 avr, 10:58, Barry <dhb2...@gmail. comwrote:
himanshu.g...@g mail.com wrote:
The following std c++ program does not output the unicode
character.:-
%./a.out
en_US.UTF-8
Infinity:
%cat unicode.cpp
#include<iostre am>
#include<string >
#include<locale >
int main()
{
std::wstring ws = L"Infinity: \u221E";
std::locale loc("");
std::cout << loc.name( ) << " " << std::endl;
std::wcout.imbu e(loc);
std::wcout << ws << std::endl;
}
[...]
What I would do in his case, for starters, is do a hex dump of
the wstring's buffer, to see exactly how L"\u221E" is encoded.
Beyond that: if it's encoded as some default character indicated
a non-supported character, then he should file an error report
with the compiler, requesting a warning, otherwise, he should
file an error report for the library, indicating that locales
aren't working as specified.
I review the standard about \u and \U.
Now I'm *sure* that my assertion about the "\u" was wrong.
I run the code, realize that (Platform : Windows XP, VC8)
dumping L"\u4e00" become "0x4e 0xA1" which is exactly UTF-16,
the default Unicode transformation on Windows.
Here, you meant 0x4E 0x00, of course. Although for Windows, I'd
expect rather 0x00 0x4E---PC's are little endian (or are you
dumping wchar_t's, converted to int, rather than char's?).
dumping "\u4e00" become "0xB6 0xA1" which is GBK encoding (mbcs),
my default encoding setting.
Is it this conversion done directly by the compiler?
Yes. The standard guarantees that in L"\uxxxx", the "xxxx" is
Unicode, regardless of your platform. The compiler decides what
it will be on your platform. In a very implementation defined
manner. It then inserts the bytes into the final string,
according to what it thinks is the equivalent of the character
in what it thinks the desired encoding should be.

As you'll note, there's a lot of "what it thinks" in there.
You're very much at the compiler's mercy.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #7
On Sat, 12 Apr 2008 23:49:15 -0700, himanshu.garg wrote:
Hi,

The following std c++ program does not output the unicode
character.:-

%./a.out
en_US.UTF-8
Infinity:
%cat unicode.cpp
#include<iostre am>
#include<string >
#include<locale >

int main()
{
std::wstring ws = L"Infinity: \u221E"; std::locale loc("");
std::cout << loc.name( ) << " " << std::endl; std::wcout.imbu e(loc);
std::wcout << ws << std::endl;
}

Have you tried using std::cout instead of std::wcout?
The above code won't print correctly for me either unless I
use std::cout and std::string.

#include<iostre am>
#include<string >
#include<locale >

int main()
{
std::string ws = "Infinity: \u221E";
std::locale loc("");
std::cout << loc.name( ) << " " << std::endl;
std::cout.imbue (loc);
std::cout << ws << std::endl;
}

:~$ ./a.out
en_US.UTF-8
Infinity: ∞

--
OU

Jun 27 '08 #8
On Sun, 13 Apr 2008 07:46:17 -0700, James Kanze wrote:
On 13 avr, 14:36, Barry <dhb2...@gmail. comwrote:
>On Apr 13, 5:59 pm, James Kanze <james.ka...@gm ail.comwrote:
On 13 avr, 10:58, Barry <dhb2...@gmail. comwrote:
himanshu.g...@g mail.com wrote:
The following std c++ program does not output the unicode
character.:-
%./a.out
en_US.UTF-8
Infinity:
%cat unicode.cpp
#include<iostre am>
#include<string >
#include<locale >
int main()
{
std::wstring ws = L"Infinity: \u221E"; std::locale loc("");
std::cout << loc.name( ) << " " << std::endl;
std::wcout.imbu e(loc);
std::wcout << ws << std::endl;
}

[...]
What I would do in his case, for starters, is do a hex dump of the
wstring's buffer, to see exactly how L"\u221E" is encoded. Beyond
that: if it's encoded as some default character indicated a
non-supported character, then he should file an error report with the
compiler, requesting a warning, otherwise, he should file an error
report for the library, indicating that locales aren't working as
specified.
>I review the standard about \u and \U. Now I'm *sure* that my assertion
about the "\u" was wrong.
>I run the code, realize that (Platform : Windows XP, VC8)
>dumping L"\u4e00" become "0x4e 0xA1" which is exactly UTF-16, the
default Unicode transformation on Windows.

Here, you meant 0x4E 0x00, of course. Although for Windows, I'd expect
rather 0x00 0x4E---PC's are little endian (or are you dumping wchar_t's,
converted to int, rather than char's?).
>dumping "\u4e00" become "0xB6 0xA1" which is GBK encoding (mbcs), my
default encoding setting.
>Is it this conversion done directly by the compiler?

Yes. The standard guarantees that in L"\uxxxx", the "xxxx" is Unicode,
regardless of your platform.
Does it?

"A character literal that begins with the letter L, such as L'x', is
a wide-character literal. A wide-character literal has type wchar_t.
The value of a wide-character literal containing a single c-char has
value equal to the numerical value of the encoding of the c-char in
the execution wide-character set. The value of the wide-character
literal containing multiple c-chars is implementation-defined."

It's implementation defined. No unicode guarantees.

Maybe it has recently been changed?

--
OU

Jun 27 '08 #9
On Apr 13, 7:29 pm, Obnoxious User <O...@127.0.0.1 wrote:
On Sat, 12 Apr 2008 23:49:15 -0700,himanshu.g argwrote:
Hi,
The following std c++ program does not output theunicode
character.:-
%./a.out
en_US.UTF-8
Infinity:
%catunicode.cpp
#include<iostre am>
#include<string >
#include<locale >
int main()
{
std::wstring ws = L"Infinity: \u221E"; std::locale loc("");
std::cout << loc.name( ) << " " << std::endl; std::wcout.imbu e(loc);
std::wcout << ws << std::endl;
}

Have you tried using std::cout instead of std::wcout?
The above code won't print correctly for me either unless I
use std::cout and std::string.

#include<iostre am>
#include<string >
#include<locale >

int main()
{
std::string ws = "Infinity: \u221E";
std::locale loc("");
std::cout << loc.name( ) << " " << std::endl;
std::cout.imbue (loc);
std::cout << ws << std::endl;

}

:~$ ./a.out
en_US.UTF-8
Infinity: $B!g(B
Hi,
Thanks for your time! I tried this one and got only second byte of
infinity codepoint in the output :-

%cat unicode.cpp
#include<iostre am>
#include<string >
#include<locale >

int main()
{
std::string ws = "\u221E";
std::locale loc("");
// std::cout << loc.name() << " " << std::endl;
std::cout.imbue (loc);
std::cout << ws << std::endl;
}

%g++ unicode.cpp
<a warning about the wide character literal>

%g++ -v
[snipped]
gcc version 3.3.1 (Mandrake Linux 9.2 3.3.1-2mdk)

% ./a.out | hexdump
0000000 0a1e
0000002

I think I'll try workarounds instead of making the application
unicode aware :(

Thank You,
Himanshu
Jun 27 '08 #10

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

Similar topics

3
17626
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code that - opens a file appropriately for output - writes to this file Thanks very much. Michael Weir
8
5285
by: Bill Eldridge | last post by:
I'm trying to grab a document off the Web and toss it into a MySQL database, but I keep running into the various encoding problems with Unicode (that aren't a problem for me with GB2312, BIG 5, etc.) What I'd like is something as simple as: CREATE TABLE junk (junklet VARCHAR(2500) CHARACTER SET UTF8)); import MySQLdb, re,urllib
8
3671
by: Francis Girard | last post by:
Hi, For the first time in my programmer life, I have to take care of character encoding. I have a question about the BOM marks. If I understand well, into the UTF-8 unicode binary representation, some systems add at the beginning of the file a BOM mark (Windows?), some don't. (Linux?). Therefore, the exact same text encoded in the same UTF-8 will result in two different binary files, and of a slightly different length. Right ?
48
4662
by: Zenobia | last post by:
Recently I was editing a document in GoLive 6. I like GoLive because it has some nice features such as: * rewrite source code * check syntax * global search & replace (through several files at once) * regular expression search & replace. Normally my documents are encoded with the ISO setting. Recently I was writing an XHTML document. After changing the encoding to UTF-8 I used the
4
6077
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3 script that grabs some web pages from the web, regex parse the data and stores it localy to xml file for further use.. at first i had no problem using python minidom and everything concerning
2
2639
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is feasible. It would be helpful if people could test the patched Python with their own applications and report any incompatibilities. PEP: 349
10
8126
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
6
7037
by: Jeff | last post by:
Hi - I'm setting up a streamreader in a VB.NET app to read a text file and display its contents in a multiline textbox. If I set it up with System.Text.Encoding.Unicode, it reads a unicode file just fine. If I set it up as ASCII, it reads a non-unicode text file. But I don't know the file format in advance. How can my app determine whether to use Unicode encoding before I read the
13
3320
by: Tomás | last post by:
Let's start off with: class Nation { public: virtual const char* GetName() const = 0; } class Norway : public Nation { public: virtual const char* GetName() const
24
9084
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special charcaters from an ASCII perspective. I get the following error: > SQLiteCur.execute(sql, row)
0
9748
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
11020
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10735
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
10358
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
7907
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
7074
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
5736
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5934
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4143
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.