Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem using Data::Translate to convert hex to decimal

Reply
Thread Tools

Problem using Data::Translate to convert hex to decimal

 
 
ajcrm125
Guest
Posts: n/a
 
      09-18-2008
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

 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      09-18-2008

Quoth ajcrm125 <(E-Mail Removed)>:
> 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 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. ] http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
ajcrm125
Guest
Posts: n/a
 
      09-18-2008
On Sep 18, 10:58*am, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth ajcrm125 <(E-Mail Removed)>:
>
> > 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 Removed)


Never even knew that function existed. Thanks!

Wonder why the h2d function is not working as expected though.
??
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      09-20-2008
On Sep 18, 11:51 am, ajcrm125 <(E-Mail Removed)> wrote:
> On Sep 18, 10:58 am, Ben Morrow <(E-Mail Removed)> wrote:
>
>
>
> > Quoth ajcrm125 <(E-Mail Removed)>:

>
> > > 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 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 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.


--
Charles DeRykus
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert from HEX to decimal Latitude and Longitude sazykin@gmail.com Java 5 08-14-2006 01:20 PM
convert hex to decimal challman@gmail.com Python 2 03-14-2006 06:31 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
How to convert an hex string to a Hex number chirs Javascript 3 12-01-2003 10:06 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments