Reputation: 13
I'm trying to convert a char
array to unicode-escapedchar
array.
Say I have a string "C:/İmüp".
How can I convert it to C:/\u0130m\u00fcp as char
array or const char
?
(I get "C:/Hello İmüp" as char array via ExpandEnvironmentStrings(), then i need to write that to a file with its unicode escapes)
I tried typecast converting, std::stringstream
and ASCII tables, looked up for examples on C++ json encoders, however i couldn't get it working
Upvotes: 1
Views: 1085
Reputation: 2184
Try this code:
string yourAsciiString = "this is test";
string yourUnicodeString = System.Text.Encoding.Unicode.GetString(System.Text.Encoding.ASCII.GetBytes(yourAsciiString));
Upvotes: -1
Reputation: 596407
Try this:
std::wstring env;
// fill env with data from ExpandEnvironmentStringsW()...
std::stringstream ss;
for (std::wstring::iterator iter = env.begin(); iter != env.end(); ++iter)
{
if (*iter <= 127)
ss << (char) *iter;
else
ss << "\\u" << std::hex << std::setfill('0') << std::setw(4) << (int)*iter;
}
std::string str = ss.str();
// use str as needed...
Upvotes: 5
Reputation: 21936
First convert it from char array to wchar_t array, using the system-default code page.
Then write trivial code that walks over your wchar_t array and escapes every Unicode character with code >= 128.
P.S. Better yet, make your application Unicode so it will use Unicode version of ExpandEnvironmentStrings. This way you will only have to escape the string, plus your app will still work correctly if some environmental string contains a character that doesn’t fit in char
with your system-default code page.
Upvotes: 2