This page looks plain and unstyled because you're using a non-standard compliant browser. To see it in its best form, please upgrade to a browser that supports web standards. It's free and painless.

Paul's Time Sink

| Main | Albums |

« | »

converting hex to binary

Paul Westbrook | 28 June, 2006 21:20

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:

[Reply]

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

slacy | 29/06/2006, 17:47

[Reply]

Um, looks my comment got truncated. Here's what I meant:

function hextobin() {
bc

slacy | 29/06/2006, 17:50

[Reply]

Grr. Truncated again.

<<<

How's that work?

slacy | 29/06/2006, 17:51

[Reply]

Ok:

function hextobin() {
bc <<EOF
obase=2
ibase=16
$@
EOF
}

Can't believe your blog doesn't let me type a less than sign!

slacy | 29/06/2006, 17:51

< character [Reply]

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

Paul Westbrook | 29/06/2006, 18:08

Add comment

Topic

Text

Your name

Your email address

Your personal page (if any)




Powered by LifeType
Design by Book of Styles