Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly convert char* into std::string? (issues while using expat / std::string(char*))
    text
    copied!<h2>Problem Description</h2> <p>I'm using Expat with a custom C++ wrapper, which I already tested on other projects. I'm running into problems, because the original data (c_str) is not converted to a std::string in the right way. This concers me, because I did not change the source of the wrapper.</p> <p>It seems like the string gets null-terminated chars after this conversion:</p> <pre><code>onCharacterData( std::string( pszData, nLength ) ) // --&gt; std::string( char* pszData) </code></pre> <p>How can I fix this? </p> <h2>Own expat wrapper</h2> <pre><code>// Wrapper defines the class Expat and implements for example: void XMLCALL Expat::CharacterDataHandler( void *pUserData, const XML_Char *pszData, int nLength ) { Expat* pThis = static_cast&lt;Expat*&gt;( pUserData ); // XML_Char is char, therefore this call contains i.e.: std::string("hello", 5) pThis-&gt;onCharacterData( std::string( pszData, nLength ) ); } </code></pre> <h2>Custom parser</h2> <pre><code>// Parser is defined as: class Parser : Expat void Parser::onCharacterData(const std::string&amp; data ) { // data is no longer char*, but a std::string. // It seems to contain \0 after each character which is wrong! // [...] } </code></pre> <h2>Character data within the expat wrapper (char*)</h2> <p><img src="https://i.stack.imgur.com/hBKrD.jpg" alt="Character data within the expat wrapper (char*)"></p> <h2>Character data within the parser (std::string)</h2> <p><img src="https://i.stack.imgur.com/jGKP9.jpg" alt="Character data within the parser (std::string)"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload