I am little poor in typecasting. I have a string in xmlChar*
(which is unsigned char*), I want to convert this unsigned char to a std::string
type.
xmlChar* name = "Some data";
I tried my best to typecast, but I couldn't find a way to convert it.
std::string sName(reinterpret_cast<char*>(name));
reinterpret_cast<char*>(name)
casts from unsigned char*
to char*
in an unsafe way but that's the one which should be used here. Then you call the ordinary constructor of std::string
.
You could also do it C-style (not recommended):
std::string sName((char*) name);
I think the accepted solution is a little bit risky and not that good to be honest. I think the better solution is using std::to_string
:
unsinged char char1{192};
auto result = std::to_string(char1)
now char1
equals to std::string("192")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.