# Problem using Data::Translate to convert hex to decimal

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

For whatever reason... the h2d is not converting some characters.
Here's an example trying to convert "09":

===================================================
use Data::Translate;
\$data = new Translate;

(\$status,\$result) = \$data->h2d("09");
print "result is \$result\n";
===================================================

\$ h2d_example.pl
result is 0

## Re: Problem using Data::Translate to convert hex to decimal

Why not just use hex()?

~% perl -le'print hex("0f")
15

Ben

--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes,        [ Heracles shoots Vulture with arrow. Vulture bursts into ]
'Alcestis')        [ flame, and falls out of sight. ]         ben@morrow.me.uk

## Re: Problem using Data::Translate to convert hex to decimal

=3D=3D=3D

=3D=3D=3D

Never even knew that function existed. Thanks!

Wonder why the h2d function is not working as expected though.
??

## Re: Problem using Data::Translate to convert hex to decimal

Hm, here's Data::Translate::h2d:

sub h2d {
shift;
local (@hex)=@_;my \$i;
for (\$i=0;\$i<=\$#hex;\$i++) {
\$hex[\$i]=ord(unpack("A",
pack("H*", \$hex[\$i])));
}
return 1,@hex;
}

Perhaps the author was intending something like:

hex[\$i]= ord(
pack("H*",sprintf "%02s",\$_));

But, as mentioned, that could be replaced  with just:

hex[\$i] = hex;

which is much better.

--
Charles DeRykus