convert unsigned char* to String

2020-02-08 06:52发布

I am little poor in type casting. 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 type cast , but I couldn't a way to convert it.

1条回答
▲ chillily
2楼-- · 2020-02-08 07:42
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);
查看更多
登录 后发表回答