[C++] UTF32からUTF16への変換

#include <codedvt>
#include <locale>

const std::u16string u32ToU16(std::u32string u32Str) {
    // codecvt_mode で little endian を指定
    std::wstring_convert<std::codecvt_utf16<uint32_t, 0x10ffff, (std::codecvt_mode)1>, uint32_t> converter;
    return reinterpret_cast<const char16_t *>(converter.to_bytes(reinterpret_cast<const uint32_t*>(u32Str.c_str())).c_str());
}

VS2015の不具合(*)によりchar32_tが使えないためuint32_tを指定しているが、LinuxやMacであればchar32_tでよいはず。

(*リンクエラーになる)

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です