Boost C++ Libraries: Ticket #7661: lexical_cast throws exception when strings have more than one null terminators https://svn.boost.org/trac10/ticket/7661 <p> Hello, </p> <p> I got following exception while using boost 1.52.0: " terminate called after throwing an instance of 'boost::bad_lexical_cast' </p> <blockquote> <p> what(): bad lexical cast: source type value could not be interpreted as target </p> </blockquote> <p> " </p> <p> Here's the sample code: " #include &lt;iostream&gt; #include &lt;boost/lexical_cast.hpp&gt; </p> <p> int main( int argc, char<strong> argv ) { </strong></p> <blockquote> <p> std::string m = "0"; m += '\0'; int i = boost::lexical_cast&lt;int&gt;( m ); std::cout &lt;&lt; "Got number " &lt;&lt; i &lt;&lt; std::endl; </p> </blockquote> <p> } " </p> <p> Thanks, Peng </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7661 Trac 1.4.3 Marshall Clow Thu, 08 Nov 2012 21:43:28 GMT <link>https://svn.boost.org/trac10/ticket/7661#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7661#comment:1</guid> <description> <p> I don't think this is a bug. </p> <p> If you catch the exception, and display the <code>what()</code>, it says:<code>bad lexical cast: source type value could not be interpreted as target</code>. </p> <p> I believe that what has happened is that it attempted to convert the two character sequence <code>"0\0"</code> to an integer, and that failed. This is the same thing that would happen if you appended (say), a 'Z' instead of a '\0'. </p> <p> Why would you expect that to succeed? </p> </description> <category>Ticket</category> </item> <item> <author>pengxu7@…</author> <pubDate>Fri, 09 Nov 2012 14:32:23 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7661#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7661#comment:2</guid> <description> <p> If you print the std::string starting with 'm' following by multiple null terminators in std::cout, you will only see "0" not "0\0". I expect boost::lexical_cast to do the same for std::string, does it make sense? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Fri, 09 Nov 2012 14:54:11 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7661#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7661#comment:3</guid> <description> <p> A <code>std::string</code> is not a null terminated string. </p> <p> The string that you have constructed is two characters long. The first character is a '0' (ASCII 48), and the second is a NUL (ASCII 0). </p> <p> When you print that string, it prints _two_ characters - it's just that your console does not display the NULL. </p> <p> If you build this program and run it, you would see "m" printed on your console. </p> <pre class="wiki">#include &lt;iostream&gt; int main( int argc, char *argv[] ) { std::string m = "0"; m += '\0'; std::cout &lt;&lt; m; } </pre><p> But if you redirect the output to a file, and look at the file, you will see that the file is two characters long, with the second being the NULL. </p> </description> <category>Ticket</category> </item> <item> <author>pengxu7@…</author> <pubDate>Sat, 10 Nov 2012 13:43:56 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7661#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7661#comment:4</guid> <description> <p> ok, pls close it as it's an invalid bug. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Sat, 10 Nov 2012 14:23:25 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/7661#comment:5 https://svn.boost.org/trac10/ticket/7661#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> Ticket