Skip to content

Instantly share code, notes, and snippets.

@kidapu
Last active August 2, 2019 15:43
Show Gist options
  • Save kidapu/c083470a39d486e31a9d49ffb546175a to your computer and use it in GitHub Desktop.
Save kidapu/c083470a39d486e31a9d49ffb546175a to your computer and use it in GitHub Desktop.
Touch Designer が日本語うけとれないらしいので?、文字列をutf16 ascii char に変換 in cpp
#include <iostream>
#include <codecvt>
#include <sstream>
// https://onlineutf8tools.com/convert-utf8-to-utf16
// https://cpprefjp.github.io/lang/cpp11/char16_32.html
using namespace std;
class ConvertUtils
{
public:
static std::string ConvertAsciiChar(std::string str)
{
wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
u16string u16str = converter.from_bytes(str);
stringstream ss;
for (char16_t c : u16str)
{
ss << std::hex << std::showbase << c;
}
return ConvertUtils::replaceString(ss.str(),"0x","\\u");
}
private:
static std::string replaceString(std::string str1, std::string str2,std::string str3)
{
std::string::size_type pos( str1.find( str2 ) );
while( pos != std::string::npos )
{
str1.replace( pos, str2.length(), str3 );
pos = str1.find( str2, pos + str3.length() );
}
return str1;
}
};
int main(int argc, const char * argv[])
{
string srcStr = "あい";
string resultSrc = ConvertUtils::ConvertAsciiChar(srcStr);
cout << resultSrc << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment