繁体   English   中英

在C ++中获取Unicode字符的CodePoint

[英]Get CodePoint of unicode Character in C++

我想获取此字符串“عربى”中每个字符的代码点,因此我编写了此代码,但它始终输出63,这是问号字符“?”的代码点。

TCHAR   myString[50] = _T("عربى");
int stringLength=_tcslen(_T(myString));

for(int i=0;i<stringLength;i++)
{
   unsigned int number =myString[i];
   cout<<number<<endl;
}

有什么建议么 ? :)

这里的代码仅使用标准库,并以32位宽的代码单元迭代字符串。 在最新的UTF-32中,它与代码点匹配。

using namespace std;
const auto str = u8"عربى";
wstring_convert<codecvt_utf8<char32_t>, char32_t> cv;
auto str32 = cv.from_bytes(str);
for(auto c : str32)
    cout << uint_least32_t(c) << '\n';

如果您的标准库尚未实现这些功能,则可能应使用外部库。

我复制了您的代码,并通过将_T(myString)强制转换为简单的myString来工作。 这是完整的程序。

#include <afxwin.h>

#include <iostream>

int main() {
    using namespace std;

    TCHAR   myString[50] = _T("عربى");
    int stringLength = _tcslen(myString); // <----- edit here

    for(int i=0;i<stringLength;i++)
    {
       unsigned int number =myString[i];
       cout<<number<<endl;
    }
}

输出:

1593
1585
1576
1609

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM