Reputation: 3871
I'm trying to convert a string to an xmlChar. The compiler says no suitable conversion function from const string to xmlChar exists. This is what the code looks like:
bool ClassName::openFile(const String& ZipFile)
{
//convert
const xmlChar *temp = (const xmlChar)ZipFile; //ZipFile has an error here. ZipFile is path and filename
...
}
Any ideas? I googled it and people were converting from xmlChar to string but not this direction.
Upvotes: 2
Views: 5082
Reputation: 41
This xml library was written for standard C. There are c style casting methods used at the time that took advantage of the BAD_CAST keyword in the library. Example:
rc = xmlTextWriterWriteElement(writer, BAD_CAST "Value1", BAD_CAST "DATA1");
if (rc < 0) {
throw EXCEPTION("Error in xmlTextWriterWriteElement");
}
But with c++ if you want to avoid the c-style cast the best you can do is:
rc = xmlTextWriterWriteElement(writer, const_cast<xmlChar*>(reinterpret_cast<const xmlChar *>("Value1")), const_cast<xmlChar*>(reinterpret_cast<const xmlChar *>("DATA1")));
Note that the usual dangers of this casting aren't a problem due to the use case here. We can safely assume the underlying library isn't going to do anything nasty to us.
Upvotes: 0
Reputation: 103
unsafe but removed overhead.
xmlChar *temp = reinterpret_cast<xmlChar*>(ZipFile);
but you must be aware of usage of reinterpret_cast
Upvotes: 0
Reputation: 6186
xmlChar
is just a typedef of unsigned char
. Just make your sentence like this:
const xmlChar *temp = ZipFile.c_str();
Upvotes: 3