{"id":152,"date":"2004-10-29T22:06:37","date_gmt":"2004-10-30T02:06:37","guid":{"rendered":"http:\/\/cherrycoated.com\/wordpress\/?p=152"},"modified":"2004-10-29T22:06:37","modified_gmt":"2004-10-30T02:06:37","slug":"about-converting-from-ascii-to-integer-integer-to-ascii-and-casting-ints-as-chars-and-chars-as-ints","status":"publish","type":"post","link":"http:\/\/www.webliminal.com\/webliminalblog\/teaching\/about-converting-from-ascii-to-integer-integer-to-ascii-and-casting-ints-as-chars-and-chars-as-ints","title":{"rendered":"About converting from ASCII to integer, integer to ASCII, and casting ints as chars and chars as ints"},"content":{"rendered":"<p>These topics came up in my <a href=\"http:\/\/people.umw.edu\/~ernie\/cpsc220\/\">CPSC 220 <\/a>class today.<br \/>\nThe course deals with C++ and we&#8217;ve worked with the string class.<\/p>\n<p>Converting from ASCII to integer and from integer to ASCII<br \/>\nneed to work with C strings rather than C++ strings.<br \/>\nThe conversion is easy, though.<br \/>\nFor this discussion suppose we have the declarations<br \/>\n#include<br \/>\nstring si, sj;<br \/>\nint i, j;<br \/>\nsi = &#8220;1234&#8221;; sj = &#8220;-3458&#8221;;<\/p>\n<p>We can convert these strings to integers by using the function <a href=\"http:\/\/www.informatik.uni-frankfurt.de\/doc\/man\/hpux\/atoi.3c.html\">ato<\/a>i as follows.<\/p>\n<p>i = atoi(si.c_str()); j = atoi(sj.c_str());<\/p>\n<p>We need to use the method c_str() of the string class, because atoi expects an argument of type char *.<\/p>\n<p>To convert a decimal integer to a string, it&#8217;s easy if you<br \/>\nuse <a href=\"http:\/\/www.informatik.uni-frankfurt.de\/doc\/man\/hpux\/sprintf.3s.html\">sprintf <\/a> as follows<br \/>\ni = 123;<br \/>\nsprintf(si.c_str(),&#8221;%d&#8221;,i);<\/p>\n<p>The integer i is stored as a decimal (%d) integer and stored<br \/>\nin the string si.<\/p>\n<p>For more info on these take a look at <a href=\"http:\/\/www.iota-six.co.uk\/c\/g3_atoi_itoa_sprintf_sscanf.asp\"><br \/>\nhttp:\/\/www.iota-six.co.uk\/c\/g3_atoi_itoa_sprintf_sscanf.asp<\/a><\/p>\n<p>Converting characters to their ASCII equivalent can be done<br \/>\nwith an explicit cast as in<br \/>\nint i;<br \/>\nstring si = &#8220;1234&#8221;;<br \/>\ni = int(si[3]);<\/p>\n<p>In the case above i gets the value 52 which is the ASCII value<br \/>\nfor &#8216;4&#8217;.<\/p>\n<p>Converting the other way is quite natural as in<br \/>\nsi[3] = char (i);<\/p>\n<p>In this case s[3] has the same value as it started with.<br \/>\nThat is,<\/p>\n<p>si[3] = char ( int (si[3]) );<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>These topics came up in my CPSC 220 class today. The course deals with C++ and we&#8217;ve worked with the string class. Converting from ASCII to integer and from integer to ASCII need to work with C strings rather than C++ strings. The conversion is easy, though. For this discussion suppose we have the declarations [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-152","post","type-post","status-publish","format-standard","hentry","category-teaching"],"_links":{"self":[{"href":"http:\/\/www.webliminal.com\/webliminalblog\/wp-json\/wp\/v2\/posts\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.webliminal.com\/webliminalblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.webliminal.com\/webliminalblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.webliminal.com\/webliminalblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.webliminal.com\/webliminalblog\/wp-json\/wp\/v2\/comments?post=152"}],"version-history":[{"count":0,"href":"http:\/\/www.webliminal.com\/webliminalblog\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.webliminal.com\/webliminalblog\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.webliminal.com\/webliminalblog\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.webliminal.com\/webliminalblog\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}