Skip to content

Instantly share code, notes, and snippets.

@BlurryLight
Created April 6, 2022 16:31
Show Gist options
  • Save BlurryLight/4c2fa990a275bc0bff92fa643b46ad03 to your computer and use it in GitHub Desktop.
Save BlurryLight/4c2fa990a275bc0bff92fa643b46ad03 to your computer and use it in GitHub Desktop.
utf8 utf16 convert
std::string utf16_to_utf8(std::u16string const &s) {
std::wstring_convert<
std::codecvt_utf8_utf16<char16_t, 0x10ffff,
std::codecvt_mode::little_endian>,
char16_t>
cnv;
std::string utf8 = cnv.to_bytes(s);
if (cnv.converted() < s.size())
throw std::runtime_error("incomplete conversion");
return utf8;
}
std::u16string utf8_to_utf16(std::string const &utf8) {
std::wstring_convert<
std::codecvt_utf8_utf16<char16_t, 0x10ffff,
std::codecvt_mode::little_endian>,
char16_t>
cnv;
std::u16string s = cnv.from_bytes(utf8);
if (cnv.converted() < utf8.size())
throw std::runtime_error("incomplete conversion");
return s;
}
// windows only
// include <windows.h>
std::u16string utf8_to_utf16_windows(std::string const &utf8) {
int count = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.c_str(),
-1, NULL, 0);
std::u16string res;
res.resize(count);
int flag = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.c_str(),
-1, (wchar_t *)(res.data()), count);
if (!flag) {
std::cerr << "converted u8 to utf16 failed" << std::endl;
return u"";
}
return res;
}
std::string utf16_to_utf8_windows(std::u16string const &utf16s) {
int count = WideCharToMultiByte(CP_UTF8, 0, (wchar_t *)(utf16s.c_str()), -1,
NULL, 0, NULL, NULL);
std::string res;
res.resize(count);
int flag = WideCharToMultiByte(CP_UTF8, 0, (wchar_t *)utf16s.c_str(), -1,
res.data(), count, NULL, NULL);
if (!flag) {
std::cerr << "converted u16 to utf8 failed" << std::endl;
return "";
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment