Thursday, July 17, 2008

The Easy Way To Convert Binary To Decimal

Hey There,

NOTE: There is no revised code in this post.

Let me get going on today's post by thanking everyone everywhere for their off-site comments the past few days, and advising you not to read tomorrow's post. It's going to be boring as @@@@, if this isn't ;)

I've been on-the-air for long enough now, that I'm going to post a "Mission Statement" that addresses the most common complaints I get on other sites and boards regarding this blog. Of course, the number one complaint is about my no-comments policy, but it will be fully explained (in granular detail) tomorrow and a link to it will be set at the top right of this blog for easy reference from that point on. In the meantime, feel free to click on the "Send me a comment" link in the upper right (below the header) corner. That will remain there as well. I've always put my email in every script I've posted, but realize that, when I go on a week long "op-ed" bender, folks probably wonder how they're supposed to contact me. It makes perfect sense. Now that I'm aware that folks are actually reading this blog and want to comment, I'll leave the email address in an easily visible place and stick with my no-comments policy, explained tomorrow or any time you want to read it after that. If I ever get off of Blogspot, I'll open up the comment floodgates (but that's for tomorrow).

Our post, from two days ago, about converting binary numbers to decimal the hard way was either widely misunderstood or its essence just poorly captured by my imagination. I know that most folks who look at a blog post with code in it will jump directly there, without reading the preamble, which is probably where I made my biggest mistake.

My second biggest mistake was to put up C code again (for the 2nd or 3rd time, I think) in an attempt to appeal to folks who were looking to take that next step and forget the mysticism surrounding it. In other words, as stated in the text leading up to the post, the code was imperfect on purpose. Top that off with the fact that I thought my original dumbed down code was too complicated and the fact that it compiled perfectly, even with a few errors in it (according to us humans, not gcc ;) and it caused quite a bit of controversy, if not some outright flaming, over at LXer, a site I like a lot (and in no way mean to defame) because I've been syndicating them since I can't remember when :) I am not, first and foremost, a C programmer by trade, so I can take the criticism (In fact, I welcome it, because it's the weakest language of the programming/scripting languages I know. Plus I've done some contracting gigs at banks and trading firms where I've been verbally assaulted for doing my job exactly correctly. If I didn't have a thick-skin for that sort of informal argument, I'd be working in a different trade by now, or be on my 5th or 6th heart attack ;)

At any point, the resulting fracas made me realize that my tendency to write volumes doesn't necessarily work well in certain types of posts. I try to frame the posts in a certain way so that each highlights a specific (or broad) aspect of itself, sometimes with the code as nothing more than a thin-vehicle to illustrate the point. Of course, if no one reads the prose, that aspect is lost.

This blog, like most blogs and their content, is provided free to anyone who wants to come by and take a look. I look at the whole thing as a learning experience for others as well as myself. I've been asked many times how I can manage to do a post a day, and the answer is "at some cost" - Not everything is as perfect as it would be if I were being paid and could take extra time. That being said, any works published here can be used as the seeds for building something better.

I use Creative Commons licensing so my site doesn't get completely ripped off "for-profit." This site is meant to be free. There will never be a charge. For that reason, although you do have my apologies and I try to maintain a high standard of quality, every once in a while, things I post on here won't be cut-and-paste simple (I even post updates - just do a site-search for "update" or "improved" and you'll find some re-posts with corrections attributed to the gracious reader who was kind enough to take the time and make me aware of a mistake I made, or a better way to get the job done).

I can assure you that every program I put up here either compiled or ran on whatever version of Unix or Linux I was running it on at the time, but I can't guarantee that they all will work on all of them. Therefore, no matter how many more years I remain in this profession and/or how much better I get, it would be ludicrous for me to ever state that I'm the "absolute authority" on anything (which, as far as I know, I never have). I've been a Unix/Linux SysAdmin for about 13 years (starting back when pretty much everyone actually believed Linux would never amount to anything) and lots has changed in that period of time. The fact that I'm not a full time programmer (just check the variety of posts and you'll get a flavour for what I do for a living) should make any reader question the content. And I'm glad that they do. Over the months, I've gained much valuable insight into things I could be doing better and I'm receptive to any criticism as long as it's not off-the-charts insulting or irrational.

That being said, here's a quicker way to convert binary numbers to decimal in the shell, although it defeats the original intent of the two previous posts (which was: trying to make uncomfortable individuals feel comfortable enough to believe they can learn to do this stuff, which they can. And please don't take that the wrong way. I only do this because I'm surrounded every day by talented people who honestly don't have the confidence in themselves or think this-and-that is far beyond their grasp, which it isn't):

host # echo 'ibase=2;11100011'|bc
227


The shortest method I know of, which, at a high-level, involves translation from binary to an ascii representation of a decimal, is to just type a number key ;)

Thank you to everyone who commented on the past few scripts and, rest assured, I'll keep your recommendations in mind and follow them as they seem necessary within the context of each individual post.

And, for those of you who wonder how I can do this every night, it took me 23 minutes to write this post. No code, just passion for writing and the subject matter. It actually takes at least twice as long to submit my work, answer questions on Forums and respond to comments (now :) Although I can't respond to them all 24 hours a day... that would suck the joy right out of this endeavour. Remember; I'm not avoiding you, I just have other things to do :)

Again, if you don't want to switch-off like a narcoleptic, avoid tomorrow's post. It's going to just be a long drawn out listing of my personal policies regarding this blog, along with explanation for each. Snooze... ;)

Best wishes,

, Mike