4tH version

Story: Perl and Bash Versions Of Binary To Decimal Conversion ScriptTotal Replies: 6
Author Content
theBeez

Jul 16, 2008
6:51 AM EDT
This is going to be a very short article:

.( 8 Digit Binary Number?) cr refill drop 0 parse 2 base ! number dup .( Binary ) . .( equals Decimal ) decimal . cr

It does the very same thing as the whole Perl script. Now I understand why the guy doesn't allow any comments.. ;-)

For those who need something more powerful, this converts any base to any base:

[needs lib/enter.4th]

: select_base decimal ." Select base : " enter dup base ! ; : get_number ." Input number: " enter ;

: show_base ( shows base without affecting base ) dup decimal [char] [ emit 0 .r [char] ] emit space base ! ;

: show_number ( shows conversion output ) rot show_base swap dup . ." => " swap show_base . ;

: convert ( converts a number ) cr select_base get_number select_base cr show_number cr ;

convert cr
shmget

Jul 16, 2008
10:42 AM EDT
If concision is your concern, then

#!/bin/bash echo "obase=2;$1" | bc

of course you can do any base to any base (within 2 - 16) using a couple more parameters like

#!/bin/bash echo "obase=${2:-2};ibase=${3:-10};$1" | bc
eggi

Jul 16, 2008
10:53 AM EDT
Hey There,

I actually don't allow comments because of comment spam, but I do include my email address (eggi@comcast.net) in every script. Trying to please google can be hard when they crawl you a lot and people post profanity or pornography, etc.

The point of the post was not to be efficient or even close to perfect, but to attempt to introduce newer users to some simple concepts in programming without overwhelming them. I doubt that someone starting out would understand your simpler explanation.

And, in the shell, it could be done with a simpler command "bc", like:

echo 'ibase=2;11110000'|bc 240

I've used binary to decimal conversion in other posts, too - The real question is "What is the intent of the post?" For instance, this older one is an introduction to using Perl's unpack function, although it does the same thing, as well:

http://linuxshellaccount.blogspot.com/2008/03/using-unpack-t...

I guess the point I'm trying to make is that the imperfection and ham-fistedness of the script was purposefully done. Now that google is crawling my site and folks are reading it, I will probably (as I noted in another thread) post a "Mission Statement" since the blog isn't about programming (in fact, it may be that I cover too much ground. Very possible).

Keep in mind that, even though I've been a Linux and Unix administrator for about 13 years, I try to write for those of us who aren't already expert enough in the field to take their skills to that next level. My blog is mostly about encouraging folks to start using Linux and Unix. Come on in. The water's fine ;)

No offense meant. Just clarifying. I try to do that in the body of the post, as well, which usually (I hope) makes more enjoyable reading ;) I think if you check out the entirety of the site, with a post per day, you'll see that coding and scripting and code (especially C) makes up a very small portion of it. I may have picked too general a topic (Unix and Linux - since I'm an administrator) but, on the plus side, it gives me a wide variety of things to write about.

I do watch these sites, though - so please feel free to comment here and I will see it about as quickly as I'd see it on my site. You can probably also catch me on any number of Unix/Linux forums (same name) that I don't think I can mention here for fear of having my remark scratched. Googling "script forum" or "unix linux forum" will take you to a couple places I consistently offer (or attempt to offer) help to other administrators on.

Apologies for any typos, but the guy who signs my paycheck wants me to do some work now ;)

Best wishes,

Mike
jezuch

Jul 16, 2008
11:41 AM EDT
Quoting:If concision is your concern, then

#!/bin/bash echo "obase=2;$1" | bc


Bah! $ echo 2i 10100111001 p | dc
eggi

Jul 16, 2008
5:20 PM EDT
touche' - there's always a better way :)

, Mike
tuxchick

Jul 17, 2008
9:13 AM EDT
When you're teaching beginners, or want to understand your code when you revisit it months later, is concise really what you want? Whether it's prose or code, seems to me clarity is more important.
eggi

Jul 17, 2008
5:49 PM EDT
Agreed... I've been stung a lot going back to old code where I thought it would be amusing to name all the variables so that they spelled out cryptic in-jokes, or made it so cryptic, in order to conserve space, that barely anyone else understood it even at the time.

Trouble-shooting those, or trying to remember where my head was at then, is nearly impossible. Easier to write from scratch, which is still a waste of time, unless it's an improvement.

I have learned that lesson over and over again ;)

, Mike

Posting in this forum is limited to members of the group: [ForumMods, SITEADMINS, MEMBERS.]

Becoming a member of LXer is easy and free. Join Us!