Wednesday, June 28, 2006

converting hex to binary


I can never remember the syntax to convert numbers between various bases with bc, and end up looking at the man page. 


Here is an example of using bc to convert a hex number to binary.




bash$ 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'.
ibase=F
obase=2
01EA

110111101



Technorati Tags:

4 comments:

  1. I think your use of "ibase=F" is a little weird, since bc defaults to base 10, it should be "ibase=16". Anyway, I've turned something similar into a bash function:
    function hextobin() {
    bc

    ReplyDelete
  2. Um, looks my comment got truncated. Here's what I meant:
    function hextobin() {
    bc

    ReplyDelete
  3. Grr. Truncated again.
    <<<
    How's that work?

    ReplyDelete
  4. I have removed a plugin that was supposed to make urls linkable. I think that was screwing things up

    ReplyDelete