如何在c ++中使用utf8字符数组? [英] How to use utf8 character arrays in c++?

查看:220
本文介绍了如何在c ++中使用utf8字符数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 char * 在C ++(VC2010)中使用utf8编码?



例如,如果我的源文件保存在utf8,我写这样:

  const char * c =aäáééő 

这是否可以让它进行utf-8编码?如果是,如何使用

  char * c2 = new char [strlen(aäáééő)];如果字符可以是可变长度,则


< =h2_lin>解决方案

窄字符串文字的编码是实现定义的,所以你真的必须阅读文档(如果你能找到它)。一个快速的实验表明,VC ++(VC8,反正)和g ++(4.4.2,反正)实际上只是复制源文件中的字节;字符串字面量将是你的编辑器保存它的任何编码。(这显然违反了标准,但它似乎是常见的做法。)



C + +11有UTF-8字符串文字,这将允许你写 u8text,并确保text以UTF-8编码。但我真的不希望它可靠地工作:问题是,为了做到这一点,编译器必须知道你的源文件的编码。在所有的可能性,编译器写者将继续忽略这个问题,只是复制源文件中的字节,并实现一致性,简单地记录源文件必须是UTF-8以使这些功能工作。


Is it possible to have char *s to work with utf8 encoding in C++ (VC2010)?

For example if my source file is saved in utf8 and I write something like this:

const char* c = "aäáéöő";

Is this possible to make it utf-8 encoded? And if yes, how is it possible to use

char* c2 = new char[strlen("aäáéöő")];

for dynamic allocation if characters can be variable length?

解决方案

The encoding for narrow character string literals is implementation defined, so you'd really have to read the documentation (if you can find it). A quick experiment shows that both VC++ (VC8, anyway) and g++ (4.4.2, anyway) actually just copy the bytes from the source file; the string literal will be in whatever encoding your editor saved it in. (This is clearly in violation of the standard, but it seems to be common practice.)

C++11 has UTF-8 string literals, which would allow you to write u8"text", and be ensured that "text" was encoded in UTF-8. But I don't really expect it to work reliably: the problem is that in order to do this, the compiler has to know what encoding your source file has. In all probability, compiler writers will continue to ignore the issue, just copying the bytes from the source file, and achieve conformance simply be documenting that the source file must be in UTF-8 for these features to work.

这篇关于如何在c ++中使用utf8字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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