• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Monday, December 20, 2010

Posted by venu k
10 comments | 6:28 AM

#!/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

10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Fantastic blog! I'm so glad to find your page.I really appreciated the concept that you have posted here. Thanks for sharing !

    ReplyDelete
  3. Decimal to Binary, Octal and HEX converter

    #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();
    }

    ReplyDelete
  4. 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 .

    ReplyDelete
  5. WebDriver 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
    . For more info on this see this link

    ReplyDelete
  6. I’ve been surfing online more than 3 hours today, yet I never found any interesting article like yours.
    It 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

    ReplyDelete
  7. I read some articles on this site and I think your blog is really interesting and has great information. Thank you for your sharing.

    ReplyDelete
  8. Thank you for sharing! Glad to hear the post, exactly what I need.
    instagram technology

    ReplyDelete
  9. 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

    ReplyDelete
  10. I 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