Login

Statistici

Membri: 150
Stiri: 729
Linkuri: 20
Vizitatori: 1266754

We support LinTube

Arata-ti suportul

Citesti stirile pe MYLRo.org? Ai un site? Pune un link catre noi.

 
Afiseaza numarul in baze diferite E-mail
  • Currently 0.0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Rating: 0.0/5 ( votes cast)

Scris de Cypress   
Thursday, 22 November 2007
#!/bin/bash
##########################################################################
# Title      :    base.sh - print number to different bases
# Author     :    Heiner Steven (stv)
# Date       :    1995-03-07
# Requires   :    bc
# Category   :    Desktop
# SCCS-Id.   :    @(#) base.sh    1.3 04/02/18
##########################################################################
# Description
#
# Changes
# 21-03-95 stv    fixed error occuring with 0xb as input (0.2)
##########################################################################

PN=`basename "$0"`            # program name
VER='1.3'

Usage () {
    echo "$PN - print number to different bases, $VER (stv '95)
usage: $PN [number ...]

If no number is given, the numbers are read from standard input.
A number may be
    binary (base 2)        starting with 0b (i.e. 0b1100)
    octal (base 8)        starting with 0  (i.e. 014)
    hexadecimal (base 16)    starting with 0x (i.e. 0xc)
    decimal            otherwise (i.e. 12)" >&2
    exit 1
}

Msg () {
    for i
    do echo "$PN: $i" >&2
    done
}

Fatal () { Msg "$@"; exit 1; }

PrintBases () {
    # Determine base of the number
    for i
    do
    case "$i" in
        0b*)        ibase=2;;    # binary
        0x*|[a-f]*|[A-F]*)    ibase=16;;    # hexadecimal
        0*)            ibase=8;;    # octal
        [1-9]*)        ibase=10;;    # decimal
        *)
        Msg "illegal number $i - ignored"
        continue;;
    esac

    # Remove prefix, convert hex digits to uppercase (bc needs this)
    number=`echo "$i" | sed -e 's:^0[bBxX]::' | tr '[a-f]' '[A-F]'`

    # Convert number to decimal
    dec=`echo "ibase=$ibase; $number" | bc`
    case "$dec" in
        [0-9]*)    ;;            # number ok
        *)        continue;;        # error: ignore
    esac

    # Print all conversions in one line
    echo `bc <<!
        obase=16; "hex="; $dec
        obase=10; "dec="; $dec
        obase=8;  "oct="; $dec
        obase=2;  "bin="; $dec
!
    ` | sed -e 's: :    :g'

    done
}

while [ $# -gt 0 ]
do
    case "$1" in
    --)    shift; break;;
    -h)    Usage;;
    -*)    Usage;;
    *)    break;;            # first number
    esac
    shift
done

if [ $# -gt 0 ]
then
    PrintBases "$@"
else                    # read from stdin
    while read line
    do
    PrintBases $line
    done
fi

Comments
Add NewSearchRSS
Only registered users can write comments!

Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved.


 

Ultimele comentarii

Softul saptamanii

WINE 1.0 RC1

Tips & Tricks

Verifica nivelul de fragmentare al fisierelor pe sisteme EXT3
Pe parcursul timpului, si sistemele Linux sufera de fragmentarea fisierelor. Pentru a verifica nivelul in care este fragmentat un anume fisier, foloseste

filefrag -v nume_fisier
 

Vizitatori Online

Avem 5 vizitatori online