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.
Use std::string when you need to store a value. Use const char * when you want maximum flexibility, as almost everything can be easily converted to or from one.
The utoa() function coverts the unsigned integer n into a character string. The string is placed in the buffer passed, which must be large enough to hold the output. The radix values can be OCTAL, DECIMAL, or HEX.
You can pass a pointer to a different kind of char , but you may need to explicitly cast it. The pointers are guaranteed to be the same size and the same values.
unsigned char is the smallest unsigned integral type, and may be used when manipulating arrays of small values on which bitwise operations are used.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With