Operating System - HP-UX
1833588 Members
4250 Online
110061 Solutions
New Discussion

Converting hex string to integer

 
SOLVED
Go to solution
Ed Kwan_2
Advisor

Converting hex string to integer

Does anybody has a script to convert a hex string to its integer equivalence ?
E.G. 2bc (string) = 700 (integer)

Thanks, Ed
13 REPLIES 13
Kent Ostby
Honored Contributor

Re: Converting hex string to integer

I usually use ADB if I need it quick.

adb
0x2bc=D
$q

best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
john korterman
Honored Contributor

Re: Converting hex string to integer

Hi,
# printf %d\\n 0x2bc

regards,
John K.
it would be nice if you always got a second chance
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Converting hex string to integer

Here is one approach leveraging bc although Perl or awk would be easy as well.

#!/usr/bin/sh

IN="2bc"
OUT=$(echo "obase=10; ibase=16; $(echo ${IN} | tr '[a-f]' '[A-F]')" | bc)
echo "IN = ${IN} OUT = ${OUT}"

Now, If I didn't miss any paren's that should work like a champ. Man bc for details. Note: bc expect hex digits to be uppercase hence the use of 'tr'.
If it ain't broke, I can fix that.
Ed Kwan_2
Advisor

Re: Converting hex string to integer

Thank you gentlemen.

However, I need to call this over and over again. I tried the following but comes back with a conversion error:

a=2bc
b=`printf %d\n $a`
echo $b

I need to use variable b as a string in other parts of my script.

Thanks, Ed
Sergejs Svitnevs
Honored Contributor

Re: Converting hex string to integer

the script:
---------------------
#! /bin/sh
H=$1
typeset -i10 D=16#$H
echo $D

---------------------

Call the script:
./script 2bc

Regards,
Sergejs

Graham Cameron_1
Honored Contributor

Re: Converting hex string to integer

#let i=16#2bc
#echo $i
700
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
john korterman
Honored Contributor

Re: Converting hex string to integer

Hi again,
try this:

a=2bc
b=$(printf %d\\n 0x${a})
echo $b

regards,
John K.
it would be nice if you always got a second chance
Graham Cameron_1
Honored Contributor

Re: Converting hex string to integer

Ok then, as a script, save the following as (eg) x2i.sh:

#/bin/sh
let i=16#$1
echo $i

#chmod u+x x2i.sj
Call it thus:
#x2i.sh 2bc
700
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Hari Kumar
Trusted Contributor

Re: Converting hex string to integer

if u have CDE(Running ReflexionX or Humming Bird)
#/usr/dt/bin/dtcalc
Sorry folks ! for being NON TECHNICAL :-)
think everybody her will excuse me
Information is Wealth ; Knowledge is Power
Ed Kwan_2
Advisor

Re: Converting hex string to integer

Thank you very much for all the help. I now have multiple ways to do it.
Chris Vail
Honored Contributor

Re: Converting hex string to integer

echo "ibase=16;2BC"|bc

This sets the input base for bc to hex, the default output is decimal. You can convert to octal and binary (or trinary, quadrary{?},
etc) by changing the argument to ibase. Go the other way by changing the obase or output base: echo "obase=16;700"|bc.
Note that if you set the ibase to a number, and want to change either the ibase or obase afterwards, you'll need to give the argument to that command in the form it is expecting it--not necessarily decimal.
Chris Vail
Honored Contributor

Re: Converting hex string to integer

Here's a quick little shell script exercise I just hacked out. It shows the outputs in decimal, octal, hexadecimal, binary, and ASCII. (no point necessary, I just did this for the halibut)
NUM=1
while test "$NUM" -le "256"
do
echo "$NUM \c"
HNUM=`echo "obase=16;$NUM"|bc`
ONUM=`echo "obase=8;$NUM"|bc`
BNUM=`echo "obase=2;$NUM"|bc`
if test "$NUM" -ge "32" -a "$NUM" -le "127"
then
ASCII=`echo "\0$ONUM"`
else
ASCII=""
fi
echo "$HNUM $ONUM $BNUM $ASCII"
(( NUM = "$NUM" + "1" ))
done