• 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:22 AM

#!/bin/bash
# SCRIPT: binary2dec.sh
# USAGE: binary2dec.sh Binary_Numbers(s)
# PURPOSE: Binary to Decimal Conversion. Takes input as command line
# arguments.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Script Starts Here #
#####################################################################
if [ $# -eq 0 ]
then
echo "Argument(s) not supplied "
echo "Usage: binary2dec.sh Binary_Number(s)"
else
echo -e "\033[1mBINARY \t\t DECIMAL\033[0m"

while [ $# -ne 0 ]
do
Binary=$1
Bnumber=$Binary
Decimal=0
power=1

while [ $Binary -ne 0 ]
do
rem=$(expr $Binary % 10 )
Decimal=$((Decimal+(rem*power)))
power=$((power*2))
Binary=$(expr $Binary / 10)
done

echo -e "$Bnumber \t\t $Decimal"
shift
done

fi

#NOTE: Using bc command you can directly get output at command line.
# Here we're converting the binary number 111 to a base 10 (decimal)
# number.
#
# $ echo 'ibase=2;obase=A;111' | bc
# 7
#
#Note that the obase is "A" and not "10".The reason for this is you've
# set the ibase to 2, so if you now had tried to use "10" as the value
# for the obase, it would stay as "2", because "10" in base 2 is "2".
# So you need to use hex to break out of binary mode or use obase=1010
#
# $ echo 'ibase=2;obase=1010;10000' | bc
# 16
# $ echo 'ibase=2;obase=1010;1000001' | bc
# 65
#
# You can omit obase=A, because default obase is 10(decimal).
# $ echo 'ibase=2;1000001' | bc
# 65

OUTPUT:
# sh binary2dec.sh 100 1111 1111111 1000000
BINARY DECIMAL
100 4
1111 15
1111111 127
1000000 64

# sh binary2dec.sh 1111111111111111111
BINARY DECIMAL
1111111111111111111 524287

# sh binary2dec.sh 10000000000000000000
BINARY DECIMAL
binary2dec.sh:line 28:[:10000000000000000000:integer expression expected
10000000000000000000

NOTE:My bash can handle upto 9223372036854775807.
But with bc command you can do more.

# echo 'ibase=2;10000000000000000000'|bc
524288
# echo 'ibase=2;110000000000000000000'|bc
1572864

10 comments:

  1. #!/bin/bash
    #convert any number of binary numbers to hex
    #use "silent" and an argument to get briefer output for subsequent arguments. Example
    #bintohex 11111 silent 111111
    if [[ ! $1 ]]
    then
    echo convert any number of binary numbers to hex
    echo use \"silent\" as an argument to get briefer output for subsequent arguments. Example
    echo $0 11111 silent 111111
    fi
    for binary in $*
    do
    if [[ "$binary" == silent ]]
    then
    silent=1
    continue
    else
    if [[ "${binary/[^01]}" != "$binary" ]]
    then
    echo "\"$binary\" is not a binary number - just use 0's and 1's"
    continue
    fi
    fi
    let value=2#$binary
    if (( ! silent ))
    then
    echo -n "binary 2#$binary = hexadecimal 0x"
    fi
    printf "%X\n" $value
    done

    ReplyDelete
  2. i want to convert hexa to decimnal please help

    ReplyDelete
  3. I won’t do too much sensual massage in london on this side first.
    Continue massaging, using varying speed, pressure and motion, all the way down on either side of the spine.
    If you also want to make sure he likes what you’re doing for the entire body.... My Web : Jasa SEO Indonesia Murah Terbaik dan Profesional

    ReplyDelete
  4. Sometimes, you have to convert binary bits into the decimal and you have to read or remember some kind of formula. It is a part of math about australianwriting but it is also used in the subject of the computer. I fully understand the nature of this article and this view is incredible.

    ReplyDelete
  5. I think a lot of people are looking for such a content from you. hope you will have more share such like that.

    ReplyDelete
  6. Thanks for sharing your information with everyone and keep posting

    ReplyDelete
  7. It’s very excellent information

    ReplyDelete
  8. This article is very much helpful and i hope this will be an useful information for the needed one.Keep on updating these kinds of informative things...


    Embedded System training in Chennai | Embedded system training institute in chennai | PLC Training institute in chennai | IEEE final year projects in chennai | VLSI training institute in chennai


    ReplyDelete
  9. These elements make the innovation not invaluable for general use, at any rate starting today. Real advocates of the innovation, nonetheless, assert that online check liquidating can be invaluable to a few, particularly to the individuals who think about time of most extreme significance.
    Cash Advance Carson

    ReplyDelete