Problem using Data::Translate to convert hex to decimal

A

ajcrm125

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

===================================================
use lib "/afs/btv.ibm.com/u/adamc/usr/lib/perl5/site_perl/5.8.5";
use Data::Translate;
$data = new Translate;

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

$ h2d_example.pl
result is 0
 
B

Ben Morrow

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

===================================================
use lib "/afs/btv.ibm.com/u/adamc/usr/lib/perl5/site_perl/5.8.5";
use Data::Translate;
$data = new Translate;

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

Why not just use hex()?

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

Ben
 
A

ajcrm125

Quoth ajcrm125 said:
For whatever reason... the h2d is not converting some characters.
Here's an example trying to convert "09":
===================================================
use lib "/afs/btv.ibm.com/u/adamc/usr/lib/perl5/site_perl/5.8.5";
use Data::Translate;
$data = new Translate;
($status,$result) = $data->h2d("09");
print "result is $result\n";
===================================================

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 thegall
   From the liver of a friend of yours. / Excuse the arrow but I haveno spoon.
(Ted Hughes,        [ Heracles shoots Vulture with arrow. Vulturebursts into ]
 'Alcestis')        [ flame, and falls out of sight. ]         (e-mail address removed)

Never even knew that function existed. Thanks!

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

C.DeRykus

Why not just use hex()?
~% perl -le'print hex("0f")
15

--
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. ] (e-mail address removed)

Never even knew that function existed. Thanks!

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top