c++ - How to properly convert char* into std::string? (issues while using expat / std::string(char*)) -
problem description
i'm using expat custom c++ wrapper, tested on other projects. i'm running problems, because original data (c_str) not converted std::string in right way. concers me, because did not change source of wrapper.
it seems string gets null-terminated chars after conversion:
oncharacterdata( std::string( pszdata, nlength ) ) // --> std::string( char* pszdata)
how can fix this?
own expat wrapper
// wrapper defines class expat , implements example: void xmlcall expat::characterdatahandler( void *puserdata, const xml_char *pszdata, int nlength ) { expat* pthis = static_cast<expat*>( puserdata ); // xml_char char, therefore call contains i.e.: std::string("hello", 5) pthis->oncharacterdata( std::string( pszdata, nlength ) ); }
custom parser
// parser defined as: class parser : expat void parser::oncharacterdata(const std::string& data ) { // data no longer char*, std::string. // seems contain \0 after each character wrong! // [...] }
character data within expat wrapper (char*)
character data within parser (std::string)
your pszdata
appears in implementation-specific unicode-derived format, each "character" takes 2 char
s.
this means source data broken; should have been wchar_t
buffer, perhaps.
Comments
Post a Comment