#!/bin/bash
# SCRIPT: dec2binary.sh
# USAGE: dec2binary.sh Decimal_Number(s)
# PURPOSE: Decimal to Binary Conversion. Takes input as command line
# arguments.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Script Starts Here #
#####################################################################
if [ $# -eq 0 ]
then
echo "Argument(s) not supplied "
echo "Usage: dec2binary.sh Decimal_number(s)"
else
echo -e "\033[1mDECIMAL \t\t BINARY\033[0m"
while [ $# -ne 0 ]
do
DecNum=$1
Binary=
Number=$DecNum
while [ $DecNum -ne 0 ]
do
Bit=$(expr $DecNum % 2)
Binary=$Bit$Binary
DecNum=$(expr $DecNum / 2)
done
echo -e "$Number \t\t $Binary"
shift
# Shifts command line arguments one step.Now $1 holds second argument
unset Binary
done
fi
#NOTE: Using bc command you can directly get output at command line.
# $ echo 'obase=2;15' | bc
# 1111
# $ echo 'obase=2;1023' | bc
# 1111111111
# $ echo 'obase=2;1024' | bc
# 10000000000
# No need of ibase=10, because default ibase is 10.
OUTPUT:
# sh dec2binary.sh 7 16 255 256 1023 1024
DECIMAL BINARY
7 111
16 10000
255 11111111
256 100000000
1023 1111111111
1024 10000000000
# sh dec2binary.sh 9223372036854775807
DECIMAL BINARY
9223372036854775807 1111111111111111111111111111111111111
11111111111111111111111111
#sh dec2binary.sh 9223372036854775808
DECIMAL BINARY
dec2binary.sh:line 13:[:9223372036854775808:integer expression expected
9223372036854775808
NOTE:My bash can handle upto 9223372036854775807.
But with bc command you can do more.
# echo 'obase=2;9223372036854775808'|bc
1000000000000000000000000000000000000000000000000000000000000000
Monday, December 20, 2010
Posted by venu k
10 comments | 6:28 AM
Subscribe to:
Post Comments (Atom)
This comment has been removed by the author.
ReplyDeleteFantastic blog! I'm so glad to find your page.I really appreciated the concept that you have posted here. Thanks for sharing !
ReplyDeleteDecimal to Binary, Octal and HEX converter
ReplyDelete#include
#include
#define MAX 79
#define ESC 27
void main()
{
char res,r;
int i,k=0,y;
long j=0,l=0,o=0;
textcolor(15);
for(i=0;i<5000;i++)
cprintf(" ");
while(1)
{
clrscr();
printf("Enter Any Number To Be Converted : ");
scanf("%ld",&j);
l=j;
o=l;
k=0;
if(j>2145555550)
{
textcolor(4);
gotoxy(15,5);
cprintf("The Number Is Greater Than Range");
textcolor(15);
getch();
goto end;
}
else
{
gotoxy(1,6);
printf("Binary Equivalent:-");
y=MAX;
while(j>0)
{
k=j%2;
j=j/2;
gotoxy(y,6);
y--;
printf("%d",k);
}
gotoxy(1,8);
printf("Hexadecimal Equivalent:- ");
y=MAX;
while(l>0)
{
k=l%16;
l=l/16;
gotoxy(y,8);
y--;
if(k<=9)
{
printf("%d",k);
}
if(k==10)
{
printf("A");
}
if(k==11)
{
printf("B");
}
if(k==12)
{
printf("C");
}
if(k==13)
{
printf("D");
}
if(k==14)
{
printf("E");
}
if(k==15)
{
printf("F");
}
}
gotoxy(1,10);
printf("Octal Equivalent:- ");
y=MAX;
while(o>0)
{
k=o%8;
o=o/8;
gotoxy(y,10);
y--;
printf("%d",k);
}
}
gotoxy(15,25);
printf("Press Any Key To Continue , ESC To Exit");
res=getch();
if(res==ESC)
{
break;
}
}
end:
textcolor(7);
for(i=0;i<5000;i++)
cprintf(" ");
clrscr();
}
Amazing post its give me good advice and also tell me how to convert math question decimal number to binary number with easy way thanks for share it graduate school statement of purpose .
ReplyDeleteWebDriver is really a different resource entirely that has various rewards over Selenium RC. The synthesis of Selenium RC and WebDriver is also known as Selenium 2. WebDriver immediately conveys with all the web browser and employs its native compatibility to automate
ReplyDelete. For more info on this see this link
I’ve been surfing online more than 3 hours today, yet I never found any interesting article like yours.
ReplyDeleteIt is pretty worth enough for me. Personally, if all webmasters and bloggers made good content as you did,
the net will be much more useful than ever before.... My Web : Master Seo Blog Coretan Review Ernawati Blog Coretan Review Ernawati
I read some articles on this site and I think your blog is really interesting and has great information. Thank you for your sharing.
ReplyDeleteThank you for sharing! Glad to hear the post, exactly what I need.
ReplyDeleteinstagram technology
In the relatively recent past, liquidating checks implied anguishing long queues in banks, just to find that your check couldn't be gotten the money for when you achieve the teller. Today, getting the money for checks just requires a couple of minutes of self-benefit from intuitive registration booths. https://www.usacheckcashingstore.com/corona
ReplyDeleteI can’t believe focusing long enough to research; much less write this kind of article. You’ve outdone yourself with this material without a doubt. It is one of the greatest contents. BinaryToday.com
ReplyDelete