Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Convert Hex to Binary in HP UX

 
Highlighted
Occasional Advisor

Convert Hex to Binary in HP UX


Hi,

In bash I can do:


$ echo -e -n "\xab\xcd\xef"|od -A n -tx1
ab cd ef
$ printf "%b" "\xab\xcd\xef"|od -A n -tx1
ab cd ef

but ksh only admits "\0", not "\x". How can I do this in ksh?


Thanks in advance,
Jose Luis
7 REPLIES 7
Highlighted
Honored Contributor

Re: Convert Hex to Binary in HP UX

Hi,

Try with binary calculator command:
#echo "ibase=16;obase=2;3E8"|bc

Check binary calculator command syntax:
#man bc

Rgds.
Highlighted
Honored Contributor

Re: Convert Hex to Binary in HP UX

Wrote this quickie awhile back....

#!/bin/ksh
#set -x
# Written: Rita Workman
#
echo "Convert DECIMAL / HEX utility:"
echo ""
echo " Enter a 1 to convert dec-to-hex "
echo " Enter a 2 to convert hex-to-dec"
echo " Enter an E to exit"

read e_code
case "$e_code" in
*Ee|Qq|quit|exit|bye) echo Quitting! See you Later; exit ;;
2)
echo "Converting hexidecimal to decimal...:"
echo ">>> Enter decimal value: "
read HEX
echo $HEX
DECV=`echo "0X$HEX=D" | /usr/bin/adb -o`
echo "DECIMAL value is: $DECV"
echo ""
;;
1)
echo "Converting decimal to hexidecimal.."
echo ">>> Enter decimal value: "
read DECV_in
echo $DECV_in
DECV=`echo "0D$DECV_in=X" | /usr/bin/adb -o`
echo "HEX value is: $DECV"
echo ""
;;
esac

Rgrds,
Rita


Highlighted
Acclaimed Contributor

Re: Convert Hex to Binary in HP UX

Hi Jose:

The bash shell differs slightly from either the 'ksh' or POSIX shell of HP-UX ('/usr/bin/sh').

That said, if you want to convert hexadecimal to binary you could do:

# perl -e 'printf qq(%#b\n), hex(shift)'

Pass the value you want converted, like:

# perl -e 'printf qq(%#b\n), hex(shift)' abcdef
0b101010111100110111101111

Regards!

...JRF...

Highlighted
Occasional Advisor

Re: Convert Hex to Binary in HP UX


I have found this solutions on Internet:


echo -n "abcdef"|perl -e 'print pack "H*", $_ for <>; '|od -A n -tx1

Regards,
Jose Luis
Highlighted
Honored Contributor

Re: Convert Hex to Binary in HP UX

if you have xwindows

bring up xhpcalc

Highlighted

Re: Convert Hex to Binary in HP UX

You can use typeset to do the conversion in a real shell:
$ typeset -i2 x=16#deadbeef; echo $x
2#11011110101011011011111011101111

ksh allows larger, unfortunately not unsigned:
$ typeset -i2 x=16#deadbeeffeedface; echo $x
-2#10000101010010010000010001000000000001000100100000010100110010
Highlighted
Acclaimed Contributor

Re: Convert Hex to Binary in HP UX

Hi (again) Jose:

> I have found this solutions on Internet:
> echo -n "abcdef"|perl -e 'print pack "H*", $_ for <>; '|od -A n -tx1

Your question was misleading. I would have framed it as how to convert a string into hexadecimal nybbles (4-bit groups of hexadecimal characters).

That said, this is shorter and more generalized:

# echo "abcdef"|perl -ne 'chomp;print pack "H*",$_'|od -A n -tx1

Regards!

...JRF...