Kyle R . Burton on Wed, 9 Apr 2003 14:07:06 -0400


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] radix conversion


> Anyone know a common utility that can convert radix?
> 
>     $ radix -b 8
>     1000
>     $ radix -h 31
>     1F
>     $
> 
> Yes, it's a very short perl program, but it's also a fundamental
> enough task that I'd be interested in a solution that I can find on
> machines I just happen to be on, without having to go find my custom
> tool. I was hoping for a flag to od, but no.

bc(1) can do that.  Use ibase to change the input base and obase to
change the output base.  Just be careful with ibase, once you set it,
setting it or obase has to be done in the base you set with ibase!

  user@host ~ $ bc
  bc 1.06
  Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
  This is free software with ABSOLUTELY NO WARRANTY.
  For details type `warranty'. 
  obase=2
  8
  1000
  obase=16
  31
  1F
  quit
  user@host ~ $
  
To convert from hex to binary:

  base=2
  ibase=16
  2FE
  1011111110
  ibase=10
  obase=10

To convert from octal to base 11:

  obase=11
  ibase=8
  137
  87


Of course bc is good for alot more than just numeric conversion.

HTH,
Kyle R. Burton

-- 

------------------------------------------------------------------------------
Wisdom and Compassion are inseparable.
        -- Christmas Humphreys
mortis@voicenet.com                            http://www.voicenet.com/~mortis
------------------------------------------------------------------------------
_________________________________________________________________________
Philadelphia Linux Users Group        --       http://www.phillylinux.org
Announcements - http://lists.netisland.net/mailman/listinfo/plug-announce
General Discussion  --   http://lists.netisland.net/mailman/listinfo/plug