Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A simple bash shell hex converter UNIX
If you have a value in hex format and want to see its decimal value, you can easily convert it using the terminal. To convert 3f hexadecimal type (in a bash shell):
 $ let x=0x3f
 $ echo $x
 63

 $ let x=0xfffe
 $ echo $x
 65534
[robg adds: Not much call for hex converters in my line of work, but I thought some of you might find this useful...]
    •    
  • Currently 4.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[67,557 views]  

A simple bash shell hex converter | 12 comments | Create New Account
Click here to return to the 'A simple bash shell hex converter' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Call bash non-interactively
Authored by: adashiel on Mar 04, '03 10:58:55AM

If you're using tcsh, rather than spawning a bash subshell for this, you can invoke bash non-interactively with a "bash -c '<command>'". Thus:

% bash -c 'let x=0x3F ; echo $x'
63



[ Reply to This | # ]
Try bc(1) too
Authored by: mkhaw on Mar 04, '03 11:46:48AM

bc is also able to do radix conversions in just about any radix (expressible in numerals and uppercase letters). For example, for hex to decimal...

tcsh% bc
ibase=16
3F
63
[ctrl-D]
tcsh% 

Or, for a shell 1-liner...

tcsh% echo "ibase=16; 3F" | bc
63
tcsh% 
Defining a csh alias or sh function named hex2dec is left as an exercise for the reader :^)!

Note (1) you have to use uppercase, (2) once you specify a non-decimal input base (via ibase=...) ALL your input is in the new radix until you change it.

bc is pretty powerful, capable of arbitrary precision math, function definition, complex C-like flow control expressions, etc. See the man page for details.



[ Reply to This | # ]
Don't forget obase
Authored by: wfolta on Mar 04, '03 12:34:14PM
You can convert from, say, octal to hex by:

obase=16
ibase=8

Note that obase is first since ibase changes your input base for all following commands, including the obase command if you reversed the order.

[ Reply to This | # ]

A simple bash shell hex converter
Authored by: cynikal on Mar 04, '03 02:47:11PM

isntead of two commands, you can do it using arithmetic expression interpolation:

echo $[0xfffe]

same thing..



[ Reply to This | # ]
/usr/bin/printf
Authored by: martinx on Mar 04, '03 05:58:46PM

printf is a bit more flexible, you can convert from hex back into decimal,
etc. It works just about like the C library function, only you run it from
the shell.

$ printf "%02X\n", 123
7B
$ printf "%d\n", 0x7B
123



[ Reply to This | # ]
A simple bash shell hex converter
Authored by: opeer on Mar 04, '03 07:33:46PM

Well, being from a linux environment, two possibly easier methods come to mind:

1) Use the command 'man ascii' shows a complete ascii table w/ conversion
from ASCII to Octal, Hex, and Integer.

2) A utility built-in to OS X is 'hexdump'. Type this at any terminal, then insert your character. Then type '^D' aka cntrl-D twice. Result shows a fake memory address and then the hex value(the first two are the ascii representation of the hex. Also for more options use 'man hexdump'



[ Reply to This | # ]
Perl version
Authored by: hayne on Mar 04, '03 09:57:58PM
Here's how you could do it as a Perl one-liner:
perl -e 'print 0x3f, "\n"'

or

perl -e 'printf("%d\n", 0x3f)'


[ Reply to This | # ]
backslashes (sigh)
Authored by: hayne on Mar 04, '03 10:01:59PM

The last "n" in both version is supposed to be "\\n" - i.e. backslash-n.

It seems the Geeklog bug with backslashes is not yet fixed - sigh.



[ Reply to This | # ]
A simple bash shell hex converter
Authored by: tfiedler on Mar 04, '03 10:32:37PM

Two bash aliases that might help out...

alias h2d='printf "%d\n" ${1}'
alias d2h='printf "0x%x\n" ${1}'

You can then just type "h2d 0xff" or "d2h 15" and get what you are looking for.



[ Reply to This | # ]
A simple bash shell hex converter
Authored by: mervTormel on Mar 04, '03 11:42:42PM

now, when did bash start allowing command line args in aliases?



[ Reply to This | # ]
And in Python
Authored by: sreeves on Mar 05, '03 10:10:30AM
Python is included with 10.2:

% python -c "print 0x3f"
63
% python -c "print hex(123)"
0x7b


[ Reply to This | # ]
A simple bash shell hex converter
Authored by: Kibe on Sep 01, '03 04:00:02PM

Well, i just found a way that is simple enough to actually remember. Type in "bc" w/o the quotes, then return in the default shell(i dont know about ne others) then change the variables "ibase" and "obase" to suit ur input and output respectivly. I.e:

[------:~] --------% bc
bc 1.05
Copyright 1991, 1992, 1993, 1994, 1997, 1998 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
obase = 16
300
12C

that is how i converted 300 into hex, to go back i wud now type this:
ibase = 16
obase = 10
12C
300

this works for any numbering system. input limits are base 2-16, output is 2-999.
so if im feeling insane i can convert base 2(binary) into base 600.
type in "man bc" for more info including how base systems higher than 16 are displayed(11-16 use lettering like hex, base 16)

this is the easiest way by far to both use and remember, not to mention actually type corectly.



[ Reply to This | # ]