{"id":137,"date":"2013-06-29T00:01:29","date_gmt":"2013-06-29T08:01:29","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=137"},"modified":"2013-06-29T06:06:05","modified_gmt":"2013-06-29T14:06:05","slug":"text-to-value-conversion","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=137","title":{"rendered":"Text to Value Conversion"},"content":{"rendered":"<p>Sure, your programs can accept values from the input stream, but most of the time numeric input comes in the form of text. The issue then becomes how to transform the string &#8220;1234&#8221; into the value 1234.<br \/>\n<!--more--><br \/>\nThe traditional C language function for converting ASCII text into an integer value is <em>atoi()<\/em>, the ASCII to Integer function. It&#8217;s still around, and it requires the <code>stdlib.h<\/code> header file to be compiler kosher. The problem with <em>atio()<\/em>, and its ASCII to floating point sister <em>atof()<\/em>, is that both have been superseded by better functions.<\/p>\n<p>Officially C language programmers don&#8217;t use the word &#8220;superseded.&#8221; They use the term <em>deprecated<\/em>, which means that a function is still around and it still works, but all the cool kids use a newer, better function. And that word again is deprecated, not depreciated.<\/p>\n<p>For converting text to integers, use the <em>strtol()<\/em> function, which I read as &#8220;string to long.&#8221;<\/p>\n<p>A similar function for converting text to floating point values is <em>strtod()<\/em>, which I would guess is &#8220;string to double.&#8221;<\/p>\n<p>As with the older functions, <em>strtol()<\/em> and <em>strtod()<\/em> require the <code>stdlib.h<\/code> header. Despite their <em>str<\/em> prefixes, they are not <code>string.h<\/code> functions. Remember that.<\/p>\n<p>Also remember that <em>strtol()<\/em> returns a <code>long int<\/code> value. So if you&#8217;re displaying that value by using the <em>printf()<\/em> function, use <code>%ld<\/code> as the conversion character. You can also typecast the value returned by using the <code>(int)<\/code> or <code>(short)<\/code> operators if you like.<\/p>\n<p>Here&#8217;s some sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    char input[64];\r\n    long value;\r\n\r\n    printf(\"Enter an integer value: \");\r\n    fgets(input,63,stdin);\r\n    value = strtol(input,NULL,10);\r\n    printf(\"You typed the value %ld.\\n\",value);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>strtol()<\/em> function requires the <code>stdlib.h<\/code> header file. It requries three arguments:<\/p>\n<ol>\n<li>A buffer where the string is stored. The function ignores any initial white space characters, but it also accepts the <code>+<\/code> or <code>-<\/code> prefix, and when a hex value is read, the prefix <code>0x<\/code> is allowed. The function stops reading the string at the first invalid numeric digit.<\/li>\n<li>The address of a buffer where the first invalid character was read. This item can be set to NULL if you don&#8217;t want to store that address. This argument allows you to save that point in the string where the number stops.<\/li>\n<li>The base of the number being read, which would normally be base 10, decimal. Other bases would be 16 or 8 for hexadecimal and octal.<\/li>\n<\/ol>\n<p>Here is a sample of <em>strtol()<\/em> where the function&#8217;s second argument is used:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    char input[64];\r\n    char *street[1];\r\n    long value;\r\n\r\n    printf(\"Enter your street address: \");\r\n    fgets(input,63,stdin);\r\n    value = strtol(input,street,10);\r\n    printf(\"Your house number is %ld\",value);\r\n    printf(\" on %s\",street[0]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>True, you can always read in values with <em>scanf()<\/em>, especially when only the value itself is input. The <em>strtol()<\/em> function, however, isn&#8217;t as finicky when it comes to sloppy input on behalf of the user.<\/p>\n<p>In the second source code example, some cleanup is necessary: The string stored at <code>street[0]<\/code> starts with the first non-valid character after the value, which is usually a space for a street address. Also, the string ends with a newline because that&#8217;s the way <em>fgets()<\/em> rolls. And, of course, house numbers are nominal numbers, meaning they&#8217;re more names than values, but that&#8217;s a minor quibble.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All the cool kids use the <em>strtol()<\/em> function to convert text input into integer values. Why not be a cool kid? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=137\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-137","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=137"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/137\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}