如何在c ++中检查字符的unicode值范围

时间:2013-11-20 18:41:10

标签: c++ unicode

我是C ++新手,来自非CS背景。因此,如果这个问题很愚蠢或者之前已经得到回答,请原谅我。

我在c ++中有一个字符串,语言是泰卢固语。

std::string str = "ఉంది"; // (it means exists; pronounced as Vundi)
std::string substring = str.substr(0,3);

上面的子字符串是“ఉ”(发音为Vu),其unicode十六进制值为0C09。

如何从子字符串中获取值0C09? 目的是检查子串是否在Telugu(0C00-0C7F)的有效范围内。

我已经看到了他们应用于obj-c,java,php,c#等的其他问题。我正在寻找使用std :: string的c ++。

根据评论,我在joelonsoftware.com/articles/Unicode.html阅读了该文章。

让我用更多信息更新我的问题。 我使用的是Fedora 19 x86_64,编码是UTF-8。控制台能够正确显示文本。

根据文章,如果我理解正确,ASCII是单字节字符,unicode是多字节字符。上面的代码示例反映了这里,每个unicode字符的长度为3个字节。除了讨论UTF-8 /文本编码和多字节字符之外,本文在检测unicode字符串的语言方面没有提供实际帮助。

可能我应该重新提出我的问题:

如何在C ++中检测unicode字符串的语言?

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

使用字符串我得到的结果是

std::string str = "ఉంది"; // (it means exists; pronounced as Vundi)
unsigned short i =str[0];
printf("%x %d",i,i);

输出“ffeo 65504”

但是当我使用wstring即

std::wstring str = L"ఉంది"; // (it means exists; pronounced as Vundi)
unsigned short i =str[0];
printf("%x %d",i,i);

输出为“c09 3081”,我认为这是正确的输出。 我不确定,但那就是你想要的。让我知道

答案 1 :(得分:0)

您可以使用ICU,也可以通过查看字符串中的连续字符来手动将UTF-8转换为UTF-16/32。有关UTF-8多字节字符的说明,请参阅here

ICU还包括unicode字符属性,这可能有用,例如用于检测脚本。

std::string没有对UTF-8到UTF-16/32转换的任何内置支持,因此substr也无法返回unicode字符。

答案 2 :(得分:0)

您需要将编码(可能是utf8)(char *)转换为宽字符(wchar_t)。

有关此转化的详情,您可以看到this postthis one