Unicode I / O. [英] Unicode I/O

查看:148
本文介绍了Unicode I / O.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




以下std c ++程序不会输出unicode

字符。: -


%。/ a.out

en_US.UTF-8

无限:

%cat unicode.cpp

#include< iostream>

#include< string>

#include< locale>


int main()

{

std :: wstring ws = L" Infinity:\ u221E";

std :: locale loc("");

std :: cout<< loc.name()<< " " << std :: endl;

std :: wcout.imbue(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 =" en_US.UTF-8"

LC_MEASUREMENT =" en_US.UTF-8"

LC_IDENTIFICATION =" en_US.UTF-8" ;

LC_ALL = en_US.UTF-8

我缺少什么?请帮助。我试图用c ++来理解unicode i / o

来修改一个应用程序以用于unicode输入。


谢谢你,

Himanshu

解决方案

hi ***********@gmail.com 写道:





以下std c ++程序不输出unicode

字符。:


%。/ a.out

en_US .UTF-8

无限:


%cat unicode.cpp

#include< iostream>

#include< string>

#include< locale>


int main()

{

std :: wstring ws = L" Infinity:\ u221E";

std :: locale loc("");

std :: cout << loc.name()<< " " << std :: endl;

std :: wcout.imbue(loc);

std :: wcout<< ws<< std :: endl;

}



当前C ++标准不包含Unicode支持,

在C ++ 0x中,会有Unicode支持。


我对于语言环境很虚伪,

但你可以查看Boost utf8_codecvt_facet ,效果很好。


我肯定没有\ u用C ++转义字符,

而Java有这个。


HTH。


-

最好的问候

Barry


13 avr,10:58,Barry< dhb2 ... @ gmail.comwrote:


himanshu.g ... @ gmail.com写道:


以下std c ++程序不输出unicode

字符。: -


%。/ a.out

en_US.UTF-8

无限:


%cat unicode.cpp

#include< iostream>

#include< string>

#include< locale>


int main()

{

std :: wstring ws = L" Infinity:\ u221E";

std :: locale loc("");

std :: cout << loc.name()<< " " << std :: endl;

std :: wcout.imbue(loc);

std :: wcout<< ws<< std :: endl;

}


当前C ++标准不包括Unicode支持,



完整的Unicode支持不存在,但有一些事情。

L" \ u221E",例如,保证作为无穷大符号

一个实现定义的默认宽字符编码,

假设它存在。并且Posix(不是C ++)保证

语言环境en_US.UTF-8。使用UTF-8编码。所以至少,从实现的质量来看,

,如果没有其他的话,

他应该从编译器得到警告(即

字符请求的字符不可用),抛出

std :: runtime_error来表示所请求的语言环境不支持
,或者他想要的角色,正确编码为
UTF-8。 (从技术上讲,locale()的行为是定义的实现
,我不认为它允许提出

异常。但是在这种情况下,使用Posix语言环境命名约定的

系统下的实现不应该返回" en_US.UTF-8"作为名称,但是相当像

" C" ;.)


在他的情况下我会做什么,对于初学者来说,是做一个十六进制转储

wstring'的缓冲区,看看究竟L" \ u221E"编码。

除此之外:如果它被编码为某个默认字符指示

a不支持的字符,那么他应该提交错误报告

与编译器,请求警告,否则,他应该为文件库提供错误报告,表明区域设置

不按指定工作。


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0) 1 30 23 00 34


4月13日,08:49,himanshu.g ... @ gmail.com写道:





* *以下std c ++程序不输出unicode

字符。: -


%。/ a.out

en_US.UTF-8

无限:


%cat unicode.cp p $ />
#include< iostream>

#include< string>

#include< locale>


int main()

{

* * std :: wstring ws = L" Infinity:\ u221E";

* * std :: locale loc("");

* * std :: cout<< loc.name()<< " " << std :: endl;

* * std :: wcout.imbue(loc);

* * std :: wcout<< ws<< std :: endl;


}



[snip]


>

我缺少什么?请帮助。我试图在c ++中理解unicode i / o

来修改应用程序以用于unicode输入。



我不知道你的问题的解决方案,但首先,我会先写一个

到一个文件,而不是控制台。使用十六进制编辑器检查文件 - 最好是

- 并验证它是否采用正确的格式。如果它是
,那么C ++部分可能没问题,你应该在其他地方找一个解决方案




/彼得


Hi,

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

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

int main()
{
std::wstring ws = L"Infinity: \u221E";
std::locale loc("");
std::cout << loc.name( ) << " " << std::endl;
std::wcout.imbue(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="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-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

解决方案

hi***********@gmail.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<iostream>
#include<string>
#include<locale>

int main()
{
std::wstring ws = L"Infinity: \u221E";
std::locale loc("");
std::cout << loc.name( ) << " " << std::endl;
std::wcout.imbue(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_facet, which works very well.

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

HTH.

--
Best Regards
Barry


On 13 avr, 10:58, Barry <dhb2...@gmail.comwrote:

himanshu.g...@gmail.com wrote:

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

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

%cat unicode.cpp
#include<iostream>
#include<string>
#include<locale>

int main()
{
std::wstring ws = L"Infinity: \u221E";
std::locale loc("");
std::cout << loc.name( ) << " " << std::endl;
std::wcout.imbue(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_error 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 objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


On 13 Apr., 08:49, himanshu.g...@gmail.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<iostream>
#include<string>
#include<locale>

int main()
{
* *std::wstring ws = L"Infinity: \u221E";
* *std::locale loc("");
* *std::cout << loc.name( ) << " " << std::endl;
* *std::wcout.imbue(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


这篇关于Unicode I / O.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆