{"id":264,"date":"2006-04-11T00:22:22","date_gmt":"2006-04-10T23:22:22","guid":{"rendered":"http:\/\/www.craigmurphy.com\/blog\/?p=264"},"modified":"2006-04-11T09:27:55","modified_gmt":"2006-04-11T08:27:55","slug":"trivial-binary-to-integer","status":"publish","type":"post","link":"http:\/\/www.craigmurphy.com\/blog\/?p=264","title":{"rendered":"Trivial: Binary To Integer"},"content":{"rendered":"<p>In response to a web-forum question, I found myself writing a little C# to convert from a string (containing a binary number) into the integer representation of the same.  It&#8217;s the kind of trivial coding problem that first year programmers would come up against&#8230;  Of course, we could make it harder by stipulating that we cannot use System.Convert.<\/p>\n<p>I&#8217;m sure that there are much better ways of doing this, and I&#8217;d be glad to see some of them appear in the comments for this post.  There, a kind of challenge&#8230;If the quality of the comments is good enough, I might be able to offer a small prize if you are a UK resident.  No promises though!<\/p>\n<p>[code lang=&#8221;C#&#8221;]<br \/>\npublic int BinToInt(string binaryNumber)<br \/>\n{<br \/>\n  int multiplier = 1;<br \/>\n  int converted = 0;<\/p>\n<p>  for (int i = binaryNumber.Length &#8211; 1; i >= 0; i&#8211;)<br \/>\n  {<br \/>\n    int t = System.Convert.ToInt16(binaryNumber[i].ToString());<br \/>\n    converted = converted + (t * multiplier);<br \/>\n    multiplier = multiplier * 2;<br \/>\n  }<br \/>\n  return converted;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>In use:<\/p>\n<p>[code lang=&#8221;C#&#8221;]<br \/>\n  \/\/ remember SEFTU, sixteen, eight, four, two, one<br \/>\n  listBox1.Items.Add(BinToInt(&#8220;00001&#8221;).ToString()); \/\/ 1<br \/>\n  listBox1.Items.Add(BinToInt(&#8220;00010&#8221;).ToString()); \/\/ 2<br \/>\n  listBox1.Items.Add(BinToInt(&#8220;00011&#8221;).ToString()); \/\/ 3<br \/>\n  listBox1.Items.Add(BinToInt(&#8220;00100&#8221;).ToString()); \/\/ 4<br \/>\n  listBox1.Items.Add(BinToInt(&#8220;00101&#8221;).ToString()); \/\/ 5<br \/>\n  listBox1.Items.Add(BinToInt(&#8220;00111&#8221;).ToString()); \/\/ 7<br \/>\n  listBox1.Items.Add(BinToInt(&#8220;01000&#8221;).ToString()); \/\/ 8<br \/>\n  listBox1.Items.Add(BinToInt(&#8220;10000&#8221;).ToString()); \/\/ 16[\/code]<\/p>\n<p>It might be of use to somebody&#8230;somewhere&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A trivial challenge, can you write a BinaryToInteger routine?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,3],"tags":[],"class_list":["post-264","post","type-post","status-publish","format-standard","hentry","category-net","category-development"],"_links":{"self":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=264"}],"version-history":[{"count":0,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/264\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}