掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
文字列の分割(UTF8) (ID:64511)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
yoh2さんのやり方のほうが効率がよいですね。 時間があったので作ってみた。 size_t utf8scbyte(const char* string) { const unsigned char c = *string; if ((c & 0x80) == 0x00) // 0*** **** return 1; if ((c & 0xE0) == 0xC0) // 110* **** return 2; if ((c & 0xF0) == 0xE0) // 1110 **** return 3; if ((c & 0xF8) == 0xF0) // 1111 0*** return 4; if ((c & 0xFC) == 0xF8) // 1111 10** return 5; if ((c & 0xFE) == 0xFC) // 1111 110* return 6; // Error return 1; } char* utf8dup(const char* utf8src, const size_t size) { char* utf8des = new char[size + 1]; memcpy(utf8des, utf8src, size + 1); return utf8des; } int main() { // ファイルから読み込む(BOMなしUTF8コード(=UTF-8N)のテキストファイル) char* utf8data = ReadUTF8Data(_T("utf8.txt")); if (utf8data) { CTypedPtrArray<CPtrArray, char*> utf8list; static const size_t N = 6; char buff[N * 6 + 1]; size_t count = 0, bytes, index = 0; for (const char* p = utf8data; *p;) { bytes = utf8scbyte(p); memcpy(&buff[index], p, bytes); p += bytes; index += bytes; if (++count >= N) { buff[index] = 0x00; utf8list.Add(utf8dup(buff, index)); index = 0; count = 0; } } if (count > 0) { buff[index] = 0x00; utf8list.Add(utf8dup(buff, index)); } // 確認のためファイルに書き出す(0x00区切りのテキスト?ファイル) WriteUTF8Data(_T("utf8_ret.txt"), utf8list); // リストの削除 for (int i = 0; i < utf8list.GetSize(); ++i) delete utf8list.GetAt(i); utf8list.RemoveAll(); // UTF8文字列の削除 delete[] utf8data; } return 0; }
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.