LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-18-2009, 05:37 PM   #1
adament1978
LQ Newbie
 
Registered: Nov 2009
Posts: 1

Rep: Reputation: 0
vim script, converting decimal to binary, 8 bit output


Hello, I'm very much a newb to linux scripts. This is no longer just an attempt to help out a fellow class mate, this is really bugging me about how to do this correctly, or intelligently.

The task was this, create a vim script that converts a decimal number to binary. The output must be in 8bit format 00000000

I figured out how to get the conversion with something like this:
n=0
bin=0
echo -n "Input decimal number to convert : "
read n
bin=`echo "obase=2;ibase=10; $n" | bc`
echo "$n is equivalent \"$bin\" in Binary"

This is mishmash of another script I found for converting hex, but I got it to work for binary.
Things I don't know: why n=0 and why bin=0,where and how to limit the output to 8 bits.
The only references we have available: internet and man pages, no book material is available to us.

The only thing I could find that might allow for an 8bit output had to do with perhaps setting the length of the output to 8; ( length 8) but I never got anything to work correctly. The guy I was trying to help ended up adding 256 lines to get it to work right, but I believe this defeats the purpose of the script, which is to make things easier. I would really appreciate anyone thoughts on this matter, as I would really like to see how it should work, thank you.
 
Old 11-18-2009, 05:46 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi,

Welcome to LQ!

The only way to achieve the desired limitation is to
reject input > 255.


Cheers,
Tink
 
Old 11-18-2009, 08:29 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,357

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Quote:
why n=0 and why bin=0
That's just being cautious by initializing vars to zero before use. Not strictly required if you know they will be written to before being used.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Perl and Bash Versions Of Binary To Decimal Conversion Script LXer Syndicated Linux News 0 07-16-2008 10:00 AM
LXer: Converting Binary Numbers To Decimal The Hard Way On Linux Or Unix LXer Syndicated Linux News 0 07-15-2008 07:11 AM
Script for converting Hexadecimal IP to Decimal IP PKrishna Linux - Newbie 5 07-04-2008 07:49 PM
Decimal to binary maldini1010 Programming 6 02-01-2005 04:03 PM
c++ binary to decimal conversion.. miguetoo Programming 13 04-24-2003 09:49 PM


All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration