I faced with pretty common problem, but I can't find solution for this. tinyxml2 library returning const char*
with Attribute(const char*)
method. In xml file, opened with that library, I have attributes with unicode and without. File converted to UTF-8. Using Linux, but it would be nice to see solution for windows too. Any suggestions?
Asked
Active
Viewed 731 times
-2

Alex
- 11
-
I'm not sure what you're asking...are you asking how to convert the `const char *` the library is returning to a `std::wstring`? – Brian Gradin Jul 28 '15 at 18:53
-
http://stackoverflow.com/questions/10737644/convert-const-char-to-wstring – Brian Gradin Jul 28 '15 at 18:54
-
Have you used swprintf() function? Take a look here [link](https://msdn.microsoft.com/en-us/library/ms235631.aspx) – Artsiom Miksiuk Jul 28 '15 at 18:56
-
@BrianGradin Ye I asked exactly what you wrote. But your link will work just for windows and I already find it. (But anyway thanks for it, because it's kinda windows solution). But now I need linux solution. – Alex Jul 28 '15 at 19:26
-
@QuestionAndAnswer Ye, I saw that link, but it's windows solution. I saw another solution lower with utf8 library, but i would solve this without libraries. (If it's impossible I will use lib) – Alex Jul 28 '15 at 19:28
1 Answers
-2
Like this:
std::wstring utf8_decode(const std::string &str)
{
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo( size_needed, 0 );
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}

Evan Carslake
- 2,267
- 15
- 38
- 56
-
-
I'm not the one who downvoted, but is that really [your code](https://stackoverflow.com/a/10738141/2302862)? – Siguza Jul 31 '15 at 08:53
-
@Siguza pretty much yeah, I put it together from google searches (as normal.) I created it when messing with GDI+ because it requires unicode. – Evan Carslake Jul 31 '15 at 15:45
-
2The code is **identical**, down to formatting, variable naming, and choice of casts as that of the [this answer](http://stackoverflow.com/a/10738141/1889329). – IInspectable Aug 03 '15 at 23:55
-
@IInspectable you're right, I guess I just didn't remember. I just remembered looking at the `MultiByteToWideChar` function in a book googling and MSDN. – Evan Carslake Aug 04 '15 at 00:00