{"id":1614,"date":"2014-12-07T03:19:34","date_gmt":"2014-12-07T03:19:34","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1614"},"modified":"2014-12-07T03:19:34","modified_gmt":"2014-12-07T03:19:34","slug":"c-convert-tofrom-string","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2014\/12\/c-convert-tofrom-string\/","title":{"rendered":"C++ convert to\/from string"},"content":{"rendered":"<p>One of the majority deficiencies in C++ has always been the lack of a simple standard method to convert to and from strings to numeric types. The C language extensions <a href=\"http:\/\/www.cplusplus.com\/reference\/cstdlib\/atoi\/\">atoi<\/a> and <a href=\"http:\/\/www.cplusplus.com\/reference\/cstdlib\/itoa\/\">itoa<\/a> are often available, but obviously only deal with integer types, and are non-standard. Cx11 appears to introduce these features in stoi and to_string methods.<\/p>\n<p>However, <a href=\"http:\/\/www.parashift.com\/c++-faq\/convert-string-to-num.html\">adapted from the C++ FAQ<\/a> this is the stringify.h header I&#8217;ve always used. It conveniently templatises the conversion (at least one example of templates not causing pain and suffering!). So you can convert a string to a double as: <\/p>\n<p>double d = convertTo&lt;double&gt;(mystring);<\/p>\n<p>Or convert a double to a string with:<\/p>\n<p>string s = stringify(d);<\/p>\n<p>#include &lt;sstream&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;stdexcept&gt;<\/p>\n<p>#ifndef STRINGIFY_H<br \/>\n#define STRINGIFY_H<\/p>\n<p>class BadConversion : public std::runtime_error {<br \/>\npublic:<br \/>\n BadConversion(const std::string&amp; s)<br \/>\n      : std::runtime_error(s) {}<br \/>\n};<\/p>\n<p>template&lt;class _type&gt;<br \/>\ninline std::string stringify(_type x)<br \/>\n{<br \/>\n  std::ostringstream o;<br \/>\n  if (!(o &lt;&lt; std::fixed &lt;&lt; x))<br \/>\n    throw BadConversion(&quot;stringify()&quot;);<br \/>\n  return o.str();<br \/>\n}<\/p>\n<p>template&lt;class _type&gt;<br \/>\ninline _type convertTo(const std::string&amp; s)<br \/>\n{<br \/>\n  std::istringstream i(s);<br \/>\n  _type x;<br \/>\n  if (!(i &gt;&gt; x))<br \/>\n    throw BadConversion(&quot;convertTo(\\&quot;&quot; + s + &quot;\\&quot;)&quot;);<br \/>\n  return x;<br \/>\n}<\/p>\n<p>#endif<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the majority deficiencies in C++ has always been the lack of a simple standard method to convert to and from strings to numeric types. The C language extensions atoi and itoa are often available, but obviously only deal with integer types, and are non-standard. Cx11 appears to introduce these features in stoi and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1614","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-q2","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=1614"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1614\/revisions"}],"predecessor-version":[{"id":1615,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1614\/revisions\/1615"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}