[c++]u16Stringからstd::ifstream を作る
エンディアンで変わるのでお気をつけて
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <istream> #include <uchar.h> #include <codecvt> #include <string> std::wstring& convertU16StringToWstring(std::wstring& ws, std::u16string& u16Str) { bool bigEndian; using namespace std; union { uint32_t b4; /* 4byte */ uint8_t b1[4]; /* 1byte×4 */ } bytes; bytes.b4 = 0x12345678; bigEndian = bytes.b1[0] == 12 ? true : false; std::u16string utf16Str = u16Str; std::wstring_convert<codecvt_utf8<uint16_t>, uint16_t> utf8Conv; if (bigEndian) { wstring_convert<codecvt_utf16<wchar_t, 0x10ffff>, wchar_t> conv; ws = conv.from_bytes( reinterpret_cast<const char*> (&utf16Str[0]), reinterpret_cast<const char*> (&utf16Str[0] + utf16Str.size())); } else { wstring_convert<codecvt_utf16<wchar_t, 0x10ffff, little_endian>, wchar_t> conv; ws = conv.from_bytes( reinterpret_cast<const char*> (&utf16Str[0]), reinterpret_cast<const char*> (&utf16Str[0] + utf16Str.size())); } return ws; } std::ifstream fin; fin.open(ws, ios::binary); |