Hardcoding a Unicode String(looks not work)

E

eric

Dear c++ advanced programers:
I copied and tried to test a piece simple code which is used for
(Hardcoding a Unicode String) from the book (
C++ Cookbook) by D. Ryan Stephens, Christopher Diggins, Jonathan
Turkanis, and Jeff Cogswell
at Chapter 13, Internationalization, section 1: Hardcoding a Unicode
String
example 13-1, it can compile and run on my g++4.5.2, but I don't quite
satisfy its result

--------------------
//Example 13-1 Hardcoding a Unicode string
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {

// Create some strings with Unicode characters
wstring ws1 = L"Infinity: \u221E";
wstring ws2 = L"Euro: \u0128";

wchar_t w[] = L"Infinity: \u221E";

wofstream out("tmp\\unicode.txt");
out << ws2 << endl;
wcout << ws2 << endl;
}


----------------------------------------------
root@eric-laptop:/home/eric/cppcookbook# ./a.out
Euro: ?
root@eric-laptop:/home/eric/cppcookbook# ls -l -a
total 56
drwxr-xr-x 2 eric eric 4096 2011-06-25 22:51 .
drwxr-xr-x 60 eric eric 4096 2011-06-25 22:51 ..
-rwxr-xr-x 1 root root 8666 2011-06-25 22:51 a.out
-rw-r--r-- 1 eric eric 7372 2011-06-25 20:07 erreta
-rw-r--r-- 1 eric eric 750 2011-05-21 09:38 example12-1.cpp
-rw-r--r-- 1 eric eric 385 2011-06-25 22:51 example13-1.cpp
-rw-r--r-- 1 eric eric 709 2011-06-25 21:12 example7-11.cpp
-rw-r--r-- 1 eric eric 366 2011-06-25 20:02 example7-12.cpp
-rw-r--r-- 1 eric eric 954 2011-06-25 19:02 example7-2.cpp
-rw-r--r-- 1 eric eric 402 2011-06-01 20:55 example9-1.cpp
-rw-r--r-- 1 root root 0 2011-06-25 22:51 tmp\unicode.txt
-rw-r--r-- 1 eric eric 564 2011-06-25 19:00 utils.h
root@eric-laptop:/home/eric/cppcookbook#
---------------------------------------------------------------------------------------------
as you can see , it didn't output to file (tmp\unicode.txt) anything.
but it did output a little thing on screen. Why is that? is that
suppose it should be on your system?
looking to see any advancer's help/hint/link and thanks a lot in
advance, Eric
 
A

Alf P. Steinbach /Usenet

* eric, on 26.06.2011 08:02:
Dear c++ advanced programers:
I copied and tried to test a piece simple code which is used for
(Hardcoding a Unicode String) from the book (
C++ Cookbook) by D. Ryan Stephens, Christopher Diggins, Jonathan
Turkanis, and Jeff Cogswell
at Chapter 13, Internationalization, section 1: Hardcoding a Unicode
String
example 13-1, it can compile and run on my g++4.5.2, but I don't quite
satisfy its result

--------------------
//Example 13-1 Hardcoding a Unicode string
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main() {

// Create some strings with Unicode characters
wstring ws1 = L"Infinity: \u221E";
wstring ws2 = L"Euro: \u0128";

According to Microsoft Word (using Alt X) the Euro sign is Unicode codepoint 0x20AC.

I find your error sort of doubly perplexing.

First, because Euro as codepoint 128 is in *Windows* ANSI. And secondly because
128 is decimal while the C++ Unicode notation is hexadecimal. How did you cross
both those barriers: from Windoze to Lunix, and from decimal to hexadecimal?

wchar_t w[] = L"Infinity: \u221E";

wofstream out("tmp\\unicode.txt");
out<< ws2<< endl;
wcout<< ws2<< endl;
}


----------------------------------------------
root@eric-laptop:/home/eric/cppcookbook# ./a.out
Euro: ?
root@eric-laptop:/home/eric/cppcookbook# ls -l -a
total 56
drwxr-xr-x 2 eric eric 4096 2011-06-25 22:51 .
drwxr-xr-x 60 eric eric 4096 2011-06-25 22:51 ..
-rwxr-xr-x 1 root root 8666 2011-06-25 22:51 a.out
-rw-r--r-- 1 eric eric 7372 2011-06-25 20:07 erreta
-rw-r--r-- 1 eric eric 750 2011-05-21 09:38 example12-1.cpp
-rw-r--r-- 1 eric eric 385 2011-06-25 22:51 example13-1.cpp
-rw-r--r-- 1 eric eric 709 2011-06-25 21:12 example7-11.cpp
-rw-r--r-- 1 eric eric 366 2011-06-25 20:02 example7-12.cpp
-rw-r--r-- 1 eric eric 954 2011-06-25 19:02 example7-2.cpp
-rw-r--r-- 1 eric eric 402 2011-06-01 20:55 example9-1.cpp
-rw-r--r-- 1 root root 0 2011-06-25 22:51 tmp\unicode.txt
-rw-r--r-- 1 eric eric 564 2011-06-25 19:00 utils.h
root@eric-laptop:/home/eric/cppcookbook#

Unicode codepoint 0x0128 is displayed as an "Ĩ" (I with tilde on top) in Word.

For an output stream an attempt is made to convert it to the
single-byte-per-character encoding used by the stream.

In Lunix that should be UTF-8, but check out wihat codepoint 0x0128 really is?

but it did output a little thing on screen. Why is that?

Dunno. Some Thorbjørning service, perhaps?

is that
suppose it should be on your system?

It's not defined by the C++ standard.

looking to see any advancer's help/hint/link and thanks a lot in
advance, Eric

Cheers & hth.,

- Alf
 
A

Alf P. Steinbach /Usenet

* Alf P. Steinbach /Usenet, on 26.06.2011 09:50:
For an output stream an attempt is made to convert it to the
single-byte-per-character encoding used by the stream.

I meant single-byte-per-representation-point.

Doesn't help that the Unicode consortium messed up the terminology here.

As a result, it's not just that I don't know the proper words: I don't even know
whether there *is* any generally-agreed-upon terminology for this. Argh.


Cheers & sorry for typo,

- Alf
 
G

Geoff

Dear c++ advanced programers:
I copied and tried to test a piece simple code which is used for
(Hardcoding a Unicode String) from the book (
C++ Cookbook) by D. Ryan Stephens, Christopher Diggins, Jonathan
Turkanis, and Jeff Cogswell
at Chapter 13, Internationalization, section 1: Hardcoding a Unicode
String
example 13-1, it can compile and run on my g++4.5.2, but I don't quite
satisfy its result

--------------------
//Example 13-1 Hardcoding a Unicode string
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {

// Create some strings with Unicode characters
wstring ws1 = L"Infinity: \u221E";
wstring ws2 = L"Euro: \u0128";

wchar_t w[] = L"Infinity: \u221E";

wofstream out("tmp\\unicode.txt");
out << ws2 << endl;
wcout << ws2 << endl;
}


----------------------------------------------
root@eric-laptop:/home/eric/cppcookbook# ./a.out
Euro: ?
root@eric-laptop:/home/eric/cppcookbook# ls -l -a
total 56
drwxr-xr-x 2 eric eric 4096 2011-06-25 22:51 .
drwxr-xr-x 60 eric eric 4096 2011-06-25 22:51 ..
-rwxr-xr-x 1 root root 8666 2011-06-25 22:51 a.out
-rw-r--r-- 1 eric eric 7372 2011-06-25 20:07 erreta
-rw-r--r-- 1 eric eric 750 2011-05-21 09:38 example12-1.cpp
-rw-r--r-- 1 eric eric 385 2011-06-25 22:51 example13-1.cpp
-rw-r--r-- 1 eric eric 709 2011-06-25 21:12 example7-11.cpp
-rw-r--r-- 1 eric eric 366 2011-06-25 20:02 example7-12.cpp
-rw-r--r-- 1 eric eric 954 2011-06-25 19:02 example7-2.cpp
-rw-r--r-- 1 eric eric 402 2011-06-01 20:55 example9-1.cpp
-rw-r--r-- 1 root root 0 2011-06-25 22:51 tmp\unicode.txt
-rw-r--r-- 1 eric eric 564 2011-06-25 19:00 utils.h
root@eric-laptop:/home/eric/cppcookbook#
---------------------------------------------------------------------------------------------
as you can see , it didn't output to file (tmp\unicode.txt) anything.
but it did output a little thing on screen. Why is that? is that
suppose it should be on your system?
looking to see any advancer's help/hint/link and thanks a lot in
advance, Eric

It is not possible to output UTF-16 using wostream. The stream will be
converted to ANSI. Your Unicode character insertions make the
conversion silently fail.
 
R

Ralf Goertz

eric said:
Dear c++ advanced programers:
I copied and tried to test a piece simple code which is used for
(Hardcoding a Unicode String) from the book (
C++ Cookbook) by D. Ryan Stephens, Christopher Diggins, Jonathan
Turkanis, and Jeff Cogswell
at Chapter 13, Internationalization, section 1: Hardcoding a Unicode
String
example 13-1, it can compile and run on my g++4.5.2, but I don't quite
satisfy its result

--------------------
//Example 13-1 Hardcoding a Unicode string
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {

// Create some strings with Unicode characters
wstring ws1 = L"Infinity: \u221E";
wstring ws2 = L"Euro: \u0128";

wchar_t w[] = L"Infinity: \u221E";

wofstream out("tmp\\unicode.txt"); out.imbue(locale(""));
out << ws2 << endl; wcout << ws2 << endl; }

Assuming you use an appropriate locale you might see what you want. I
get (with LANG=de_DE.UTF-8):

Euro: Ĩ

in the outfile. You should also imbue wcout and set ios::sync_with_stdio
to false.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top