Operating System - HP-UX
1752275 Members
4894 Online
108786 Solutions
New Discussion юеВ

Re: Convert Hex to Binary in HP UX

 
jose_luis_fdez_diaz
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
Jose Mosquera
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.
Rita C Workman
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


James R. Ferguson
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...

jose_luis_fdez_diaz
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
Emil Velez
Honored Contributor

Re: Convert Hex to Binary in HP UX

if you have xwindows

bring up xhpcalc

Dennis Handly
Acclaimed Contributor

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
James R. Ferguson
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...