Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a context where I need to convert binary to hexadecimal and decimal and viceversa in a shell script. Can someone suggest me a tool for this?

share|improve this question
2  
Seeing the recent answers, maybe you should specify whether “binary” means binary number or binary file. –  manatwork Feb 19 '13 at 7:08

5 Answers 5

up vote 9 down vote accepted

It's fairly straightforward to do the conversion from binary in pure bash (printf is a builtin):

Binary to decimal

$ echo "$((2#101010101))"
341

Binary to hexadecimal

$ printf '%x\n' "$((2#101010101))"
155

Going back to binary using bash alone is somewhat more complex, so I suggest you see the other answers for solutions to that.

share|improve this answer
    
how about decimal to hexadecimal. –  Bangi Feb 19 '13 at 8:54
    
@Bangi Just do printf '%x\n' 10, for example. –  Chris Down Feb 19 '13 at 10:14
    
What's the point of using printf %d for bin2dec? $(( ... )) already gives a decimal result, echo is enough. –  Andrea Corbellini Feb 19 '13 at 13:42
    
@AndreaCorbellini - Personal preference. In general, I use printf '%s\n' foo instead of echo for a variety of reasons (mostly portability), for the same reason, I don't use it here. –  Chris Down Feb 20 '13 at 5:24
    
@ChrisDown: I though this question was explicitly about Bash (which has a well-implemented echo builtin). My bad! –  Andrea Corbellini Feb 20 '13 at 9:49

Assuming that by binary, you mean binary data as in data with any possible byte value including 0, and not base-2 numbers:

To convert from binary, od (standard), xxd (comes with vim) or perl's unpack come to mind.

od -An -vtu1 # for decimal
od -An -vtx1 # for hexadecimal

xxd -p # for hexa

perl -pe 'BEGIN{$\="\n";$/=\30};$_=unpack("H*",$_)' # like xxd -p

# for decimal:
perl -ne 'BEGIN{$\="\n";$/=\30;$,=" "}; print unpack("C*",$_)'

Now, to convert back to binary, awk (standard), xxd -r or perl's pack:

From the decimal output from od -tu1 or perl above:

awk '{for (i = 1; i <= NF; i++) printf "%c", $i}'
perl -ape '$_=pack("C*",@F)'

From the hexa perl or xxd -p above:

xxd -r -p
perl -pe 'chomp;$_=pack("H*",$_)'
share|improve this answer

You can use bc for this by manipulating the ibase and obase parameters:

The trick is that you need to be explicit about the bases. So if your ibase is 2, then if you set your obase to 10, it won't do anything, as 10 in binary is 2. Hence you need to use hexadecimal notation.

So binary to decimal would be (watch that obase is A)

Binary to decimal:

$> echo 'ibase=2;obase=A;11110001011010'|bc
15450

Binary to hex:

$> echo 'ibase=2;obase=10000;11110001011010'|bc
3C5A
share|improve this answer
4  
This second example is wrong -- 'F' would be base 15, not 16 (decimal 16 in hex is 10, not F). Try echo 'ibase=2;obase=F;1111 which should equal decimal 15, ie. F in hex. It comes out as 10, which it is in base 15 (digits 0-E). It is also easier if you state the obase first, eg: echo 'obase=16;ibase=2;1111'|bc. No confusion. –  goldilocks Feb 19 '13 at 7:21
    
@goldilocks thanks, you're correct, fixed. –  psarossy Feb 26 '13 at 18:03
1  
Now it is worst. Once you set ibase, you have to provide input in that base, even for obase. So in your example would be echo 'ibase=2;obase=10000;11110001011010'|bc. Better listen to goldilocks's advice and reverse the order – first set obase, then ibase. –  manatwork Feb 26 '13 at 18:08

for binary to hexadecimal use

xxd tool in linux and for binary to decimal you can use qalculate tool.

for help regarding xxd type xxd --help or man xxd in linux.

share|improve this answer

If you mean converting numbers from base-2 to 10 or 16 and back, bc is the standard tool to do that as already mentioned by psarossy.

decimal=123
binary=$(echo "obase=2;$decimal" | bc)
hex=$(echo "obase=16;ibase=2;$binary" | bc)

Some shells like zsh have builtin support for base conversion as part of their arithmetic expansion operators:

decimal=123
binary=$(([##2]decimal))
hex=$(([##16]decimal))
hex=$(([##16]2#$binary))
decimal=$((2#$binary))

and so on.

Both ksh93 and zsh also support:

typeset -i2 binary=123
typeset -i16 dec2hex=123 bin2hex='2#1111'

But note that, when expanded, $binary will have a 2# or 16# prefix (which you can strip with ${binary#*#}.

ksh93 also supports:

printf "%..2d\n" 123

to convert to binary.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.