1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Frequently Asked Questions</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../boost_lexical_cast.html" title="Chapter 21. Boost.Lexical_Cast 1.0"> 10<link rel="prev" href="synopsis.html" title="Synopsis"> 11<link rel="next" href="changes.html" title="Changes"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="synopsis.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_lexical_cast.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="changes.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="boost_lexical_cast.frequently_asked_questions"></a><a class="link" href="frequently_asked_questions.html" title="Frequently Asked Questions">Frequently 29 Asked Questions</a> 30</h2></div></div></div> 31<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 32 <span class="bold"><strong>Question:</strong></span> Why does <code class="computeroutput"><span class="identifier">lexical_cast</span><span class="special"><</span><span class="identifier">int8_t</span><span class="special">>(</span><span class="string">"127"</span><span class="special">)</span></code> throw <code class="computeroutput"><span class="identifier">bad_lexical_cast</span></code>? 33 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"> 34 <span class="bold"><strong>Answer:</strong></span> The type <code class="computeroutput"><span class="identifier">int8_t</span></code> 35 is a <code class="computeroutput"><span class="keyword">typedef</span></code> to <code class="computeroutput"><span class="keyword">char</span></code> or <code class="computeroutput"><span class="keyword">signed</span> 36 <span class="keyword">char</span></code>. Lexical conversion to 37 these types is simply reading a byte from source but since the source 38 has more than one byte, the exception is thrown. Please use other 39 integer types such as <code class="computeroutput"><span class="keyword">int</span></code> 40 or <code class="computeroutput"><span class="keyword">short</span> <span class="keyword">int</span></code>. 41 If bounds checking is important, you can also call <a href="../../../libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric_cast</span></code></a>: <code class="computeroutput"><span class="identifier">numeric_cast</span><span class="special"><</span><span class="identifier">int8_t</span><span class="special">>(</span><span class="identifier">lexical_cast</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="string">"127"</span><span class="special">));</span></code> 42 </li></ul></div> 43 </li></ul></div> 44<pre class="programlisting"></pre> 45<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 46 <span class="bold"><strong>Question:</strong></span> Why does <code class="computeroutput"><span class="identifier">lexical_cast</span><span class="special"><</span><span class="keyword">unsigned</span> <span class="keyword">char</span><span class="special">>(</span><span class="string">"127"</span><span class="special">)</span></code> 47 throw <code class="computeroutput"><span class="identifier">bad_lexical_cast</span></code>? 48 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"> 49 <span class="bold"><strong>Answer:</strong></span> Lexical conversion to any 50 char type is simply reading a byte from source. But since the source 51 has more than one byte, the exception is thrown. Please use other 52 integer types such as <code class="computeroutput"><span class="keyword">int</span></code> 53 or <code class="computeroutput"><span class="keyword">short</span> <span class="keyword">int</span></code>. 54 If bounds checking is important, you can also call <a href="../../../libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric_cast</span></code></a>: <code class="computeroutput"><span class="identifier">numeric_cast</span><span class="special"><</span><span class="keyword">unsigned</span> <span class="keyword">char</span><span class="special">>(</span><span class="identifier">lexical_cast</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="string">"127"</span><span class="special">));</span></code> 55 </li></ul></div> 56 </li></ul></div> 57<pre class="programlisting"></pre> 58<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 59 <span class="bold"><strong>Question:</strong></span> What does <code class="computeroutput"><span class="identifier">lexical_cast</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></span></code> of an <code class="computeroutput"><span class="identifier">int8_t</span></code> 60 or <code class="computeroutput"><span class="identifier">uint8_t</span></code> not do what 61 I expect? 62 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"> 63 <span class="bold"><strong>Answer:</strong></span> As above, note that int8_t 64 and uint8_t are actually chars and are formatted as such. To avoid 65 this, cast to an integer type first: <code class="computeroutput"><span class="identifier">lexical_cast</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">>(</span><span class="keyword">static_cast</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">n</span><span class="special">));</span></code> 66 </li></ul></div> 67 </li></ul></div> 68<pre class="programlisting"></pre> 69<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 70 <span class="bold"><strong>Question:</strong></span> The implementation always resets 71 the <code class="computeroutput"><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">skipws</span></code> flag of an underlying stream object. 72 It breaks my <code class="computeroutput"><span class="keyword">operator</span><span class="special">>></span></code> 73 that works only in presence of this flag. Can you remove code that resets 74 the flag? 75 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"> 76 <span class="bold"><strong>Answer:</strong></span> May be in a future version. 77 There is no requirement in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1973.html" target="_top">Lexical 78 Conversion Library Proposal for TR2, N1973 by Kevlin Henney and Beman 79 Dawes</a> to reset the flag but remember that <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1973.html" target="_top">Lexical 80 Conversion Library Proposal for TR2, N1973</a> is not yet accepted 81 by the committee. By the way, it's a great opportunity to make your 82 <code class="computeroutput"><span class="keyword">operator</span><span class="special">>></span></code> 83 more general. Read a good C++ book, study <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">sentry</span></code> 84 and <a href="../../../libs/io/doc/ios_state.html" target="_top"><code class="computeroutput"><span class="identifier">ios_state_saver</span></code></a>. 85 </li></ul></div> 86 </li></ul></div> 87<pre class="programlisting"></pre> 88<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 89 <span class="bold"><strong>Question:</strong></span> Why <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span><span class="special"><</span><span class="keyword">unsigned</span> <span class="keyword">int</span><span class="special">>(</span><span class="string">"-1"</span><span class="special">);</span></code> 90 does not throw, but outputs 4294967295? 91 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"> 92 <span class="bold"><strong>Answer:</strong></span> <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code> 93 has the behavior of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">stringstream</span></code>, 94 which uses <code class="computeroutput"><span class="identifier">num_get</span></code> 95 functions of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">locale</span></code> 96 to convert numbers. If we look at the Programming languages — C++, 97 we'll see, that <code class="computeroutput"><span class="identifier">num_get</span></code> 98 uses the rules of <code class="computeroutput"><span class="identifier">scanf</span></code> 99 for conversions. And in the C99 standard for unsigned input value 100 minus sign is optional, so if a negative number is read, no errors 101 will arise and the result will be the two's complement. 102 </li></ul></div> 103 </li></ul></div> 104<pre class="programlisting"></pre> 105<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 106 <span class="bold"><strong>Question:</strong></span> Why <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">L</span><span class="char">'A'</span><span class="special">);</span></code> outputs 107 65 and <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span><span class="special"><</span><span class="keyword">wchar_t</span><span class="special">>(</span><span class="identifier">L</span><span class="string">"65"</span><span class="special">);</span></code> does not throw? 108 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"> 109 <span class="bold"><strong>Answer:</strong></span> If you are using an old 110 version of Visual Studio or compile code with /Zc:wchar_t- flag, 111 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code> sees single <code class="computeroutput"><span class="keyword">wchar_t</span></code> character as <code class="computeroutput"><span class="keyword">unsigned</span> <span class="keyword">short</span></code>. 112 It is not a <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code> 113 mistake, but a limitation of compiler options that you use. 114 </li></ul></div> 115 </li></ul></div> 116<pre class="programlisting"></pre> 117<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 118 <span class="bold"><strong>Question:</strong></span> Why <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span><span class="special"><</span><span class="keyword">double</span><span class="special">>(</span><span class="string">"-1.#IND"</span><span class="special">);</span></code> throws <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">bad_lexical_cast</span></code>? 119 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"> 120 <span class="bold"><strong>Answer:</strong></span> <code class="computeroutput"><span class="string">"-1.#IND"</span></code> 121 is a compiler extension, that violates standard. You shall input 122 <code class="computeroutput"><span class="string">"-nan"</span></code>, <code class="computeroutput"><span class="string">"nan"</span></code>, <code class="computeroutput"><span class="string">"inf"</span></code> 123 , <code class="computeroutput"><span class="string">"-inf"</span></code> (case 124 insensitive) strings to get NaN and Inf values. <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span><span class="special"><</span><span class="identifier">string</span><span class="special">></span></code> outputs <code class="computeroutput"><span class="string">"-nan"</span></code>, 125 <code class="computeroutput"><span class="string">"nan"</span></code>, <code class="computeroutput"><span class="string">"inf"</span></code>, <code class="computeroutput"><span class="string">"-inf"</span></code> 126 strings, when has NaN or Inf input values. 127 </li></ul></div> 128 </li></ul></div> 129<pre class="programlisting"></pre> 130<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 131 <span class="bold"><strong>Question:</strong></span> What is the fastest way to convert 132 a non zero terminated string or a substring using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lexical_cast</span></code>? 133 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"> 134 <span class="bold"><strong>Answer:</strong></span> Use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">iterator_range</span></code> 135 for conversion or <code class="computeroutput"><span class="identifier">lexical_cast</span></code> 136 overload with two parameters. For example, if you whant to convert 137 to <code class="computeroutput"><span class="keyword">int</span></code> two characters 138 from a string <code class="computeroutput"><span class="identifier">str</span></code>, 139 you shall write <code class="computeroutput"><span class="identifier">lexical_cast</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">make_iterator_range</span><span class="special">(</span><span class="identifier">str</span><span class="special">.</span><span class="identifier">data</span><span class="special">(),</span> <span class="identifier">str</span><span class="special">.</span><span class="identifier">data</span><span class="special">()</span> <span class="special">+</span> <span class="number">2</span><span class="special">));</span></code> 140 or <code class="computeroutput"><span class="identifier">lexical_cast</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">str</span><span class="special">.</span><span class="identifier">data</span><span class="special">(),</span> 141 <span class="number">2</span><span class="special">);</span></code>. 142 </li></ul></div> 143 </li></ul></div> 144</div> 145<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 146<td align="left"></td> 147<td align="right"><div class="copyright-footer">Copyright © 2000-2005 Kevlin Henney<br>Copyright © 2006-2010 Alexander Nasonov<br>Copyright © 2011-2020 Antony Polukhin<p> 148 Distributed under the Boost Software License, Version 1.0. (See accompanying 149 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 150 </p> 151</div></td> 152</tr></table> 153<hr> 154<div class="spirit-nav"> 155<a accesskey="p" href="synopsis.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_lexical_cast.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="changes.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 156</div> 157</body> 158</html> 159