Tux

...making Linux just a little more fun!

Two-cent Tip: number conversion between between base-x to base-y

Mulyadi Santosa [mulyadi.santosa at gmail.com]


Sat, 18 Jul 2009 20:35:59 +0700

For those who needs quick help on number conversion between base-x to base-y, bc can lend a help here. For example, what's the hexadecimal form of 116?:

$ echo 'obase=16; 116' | bc
74

And what does binary 11010101 look in decimal?

$ echo 'ibase=2; 11010101' | bc
213

By doing below step, you directly convert binary 11010101 to hexadecimal:

$ echo 'obase=16; ibase=2; 11010101' | bc
D5

Note that "obase" must preced "ibase" in order to make a correct final result.

regards,

-- 
Mulyadi Santosa
Freelance Linux trainer
blog: the-hydra.blogspot.com


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Sat, 18 Jul 2009 17:27:42 +0100

2009/7/18 Mulyadi Santosa <mulyadi.santosa@gmail.com>:

> For those who needs quick help on number conversion between base-x to
> base-y, bc can lend a help here. For example, what's the hexadecimal
> form of 116?:
> $ echo 'obase=16; 116' | bc
> 74

Or as a semi-equivalent, GNU AWK has strtonum() and you can of course do the whole lot in Perl. (Which is arguarbly more portable than using bc or dc.)

-- Thomas Adam


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Sat, 18 Jul 2009 12:55:53 -0500

On Sat, Jul 18, 2009 at 05:27:42PM +0100, Thomas Adam wrote:

> 2009/7/18 Mulyadi Santosa <mulyadi.santosa@gmail.com>:
> > For those who needs quick help on number conversion between base-x to
> > base-y, bc can lend a help here. For example, what's the hexadecimal
> > form of 116?:
> > $ echo 'obase=16; 116' | bc
> > 74
> 
> Or as a semi-equivalent, GNU AWK has strtonum() and you can of course
> do the whole lot in Perl.  (Which is arguarbly more portable than
> using bc or dc.)

ben@Jotunheim:~$ printf '%x\n' 116
74
ben@Jotunheim:~$ printf '%o\n' 116
164
ben@Jotunheim:~$ printf '%e\n' 256347
2.563470e+05

Quick review (your version of Bash may support other conversions):

  %o   an unsigned integer, in octal
  %x   an unsigned integer, in hexadecimal
  %e   a floating-point number, in scientific notation
  %f   a floating-point number, in fixed decimal notation
  %g   a floating-point number, in %e or %f notation
  %X   like %x, but using upper-case letters
  %E   like %e, but using an upper-case "E"
  %G   like %g, but with an upper-case "E" (if applicable)

As Thomas points out, however, for OS and shell-independent conversion, there's always Perl. 'bc' is also useful, though, whenever you need a quick bit of math processing in a shell script.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Mulyadi Santosa [mulyadi.santosa at gmail.com]


Sun, 19 Jul 2009 21:15:47 +0700

On Sun, Jul 19, 2009 at 12:55 AM, Ben Okopnik<ben@linuxgazette.net> wrote:

> Quick review (your version of Bash may support other conversions):
>
> ``
> %o  an unsigned integer, in octal
> %x  an unsigned integer, in hexadecimal
> %e  a floating-point number, in scientific notation
> %f  a floating-point number, in fixed decimal notation
> %g  a floating-point number, in %e or %f notation
> %X  like %x, but using upper-case letters
> %E  like %e, but using an upper-case "E"
> %G  like %g, but with an upper-case "E" (if applicable)
> ''

Ouch, didn't know that before... thanks for pointing that out! :)

regards,

-- 
Mulyadi Santosa
Freelance Linux trainer
blog: the-hydra.blogspot.com


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Sun, 19 Jul 2009 13:03:15 -0500

On Sun, Jul 19, 2009 at 09:15:47PM +0700, Mulyadi Santosa wrote:

> On Sun, Jul 19, 2009 at 12:55 AM, Ben Okopnik<ben@linuxgazette.net> wrote:
> > Quick review (your version of Bash may support other conversions):
> >
> > ``
> > %o  an unsigned integer, in octal
> > %x  an unsigned integer, in hexadecimal
> > %e  a floating-point number, in scientific notation
> > %f  a floating-point number, in fixed decimal notation
> > %g  a floating-point number, in %e or %f notation
> > %X  like %x, but using upper-case letters
> > %E  like %e, but using an upper-case "E"
> > %G  like %g, but with an upper-case "E" (if applicable)
> > ''
> 
> Ouch, didn't know that before... thanks for pointing that out! :)

That shouldn't be an "ouch" - it's useful! :)

The "printf" in Bash is a rather useful gadget - in its basic form, it's a shell-independent command that works the same way everywhere (unlike, e.g., "echo".) It also has a whole lot of formatting and conversion magic - and it's made even more useful by its '-v' argument; this allows you to use it as the equivalent of "sprintf". That is, you don't have to use command substitution on "printf" to assign its output to a variable:

# Instead of this...
hex_num=$(printf "0x%x" 1742)
 
# ...do this:
printf -c hex_num "0x%x" 1742
-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Mulyadi Santosa [mulyadi.santosa at gmail.com]


Mon, 20 Jul 2009 12:15:53 +0700

On Mon, Jul 20, 2009 at 1:03 AM, Ben Okopnik<ben@linuxgazette.net> wrote:

> That shouldn't be an "ouch" - it's useful! :)
>
> The "printf" in Bash is a rather useful gadget - in its basic form, it's
> a shell-independent command that works the same way everywhere (unlike,
> e.g., "echo".) It also has a whole lot of formatting and conversion
> magic - and it's made even more useful by its '-v' argument; this allows
> you to use it as the equivalent of "sprintf". That is, you don't have
> to use command substitution on "printf" to assign its output to a
> variable:
>
> ```
> # Instead of this...
> hex_num=$(printf "0x%x" 1742)
>
> # ...do this:
> printf -c hex_num "0x%x" 1742
> '''

I see. Several months ago, when someone talked about printf in bash, I was thinking he/she was referring to C function. OK, now I know more about printf in bash. Very useful, will keep them in mind!

regards,

-- 
Mulyadi Santosa
Freelance Linux trainer
blog: the-hydra.blogspot.com


Top    Back