如何在不使用 qt 的情况下知道 C++ 中的 unicode 块?

How to know unicode block in c++ without using qt?

我有一个包含日语字符的文件,我想知道该行是否仅包含片假名字符而不使用 Qtcore

trial.txt 包含:

こにちわ
おはよう
ナルト

我想让程序说第三行全是片假名

文件保存为"UTF-8 Unicode text, with CRLF line terminators"。

如果您认为这是一个重复的问题,请将 link 评论到同一个已回答的问题。

/*
Unicode Ranges:
3040 — 309F     Hiragana
30A0 — 30FF     Katakana
*/

我正在使用 C++,Visual Studio 2013,gcc 4.8.3,我当前的代码页是 Unicode(带签名的 UTF-8)。 u8 之类的前缀不起作用(我不知道为什么,它应该起作用)。

我编辑了我在研究这个问题时发现的 2 个代码。

我决定采纳 Joachim Pileborg 的建议将文件解码为 UTF-32,并且我使用 UTF-32 十进制值来设置范围

        //conversion from http://en.cppreference.com/w/cpp/locale/wstring_convert/converted
        void utf8ToUtf32(string line){
            string utf8 = line; 

            // the UTF-8 - UTF-32 standard conversion facet
            wstring_convert<codecvt_utf8<char32_t>, char32_t> cvt;

            // UTF-8 to UTF-32
            u32string utf32 = cvt.from_bytes(utf8);

            //printing of decimal val inspired by http://www.cs.ucr.edu/~cshelton/courses/cppsem/strex.cc
            cout << utf32.length() << ": ";
            for (char32_t c : utf32) {
                cout << hex  << c << ' ';
                writeFile << c << ' ';

                if (c >= 12450 && c <= 12543) cout << "k ";
            }
            cout << dec << endl;
            writeFile << dec << endl;
        }

我知道可能还有其他方法可以做到这一点,但在我的时间范围内,这已经足够了。