Problem using Data::Translate to convert hex to decimal

Discussion in 'Perl Misc' started by ajcrm125, Sep 18, 2008.

  1. ajcrm125

    ajcrm125 Guest

    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
     
    ajcrm125, Sep 18, 2008
    #1
    1. Advertising

  2. ajcrm125

    Ben Morrow Guest

    Quoth 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";
    > ===================================================


    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, Sep 18, 2008
    #2
    1. Advertising

  3. ajcrm125

    ajcrm125 Guest

    On Sep 18, 10:58 am, Ben Morrow <> wrote:
    > Quoth 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";
    > > ===================================================

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


    Never even knew that function existed. Thanks!

    Wonder why the h2d function is not working as expected though.
    ??
     
    ajcrm125, Sep 18, 2008
    #3
  4. ajcrm125

    C.DeRykus Guest

    On Sep 18, 11:51 am, ajcrm125 <> wrote:
    > On Sep 18, 10:58 am, Ben Morrow <> wrote:
    >
    >
    >
    > > Quoth 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";
    > > > ===================================================

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

    >
    > 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
     
    C.DeRykus, Sep 20, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,293
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    494
    Juha Autero
    Aug 19, 2003
  3. convert hex to decimal

    , Mar 9, 2006, in forum: Python
    Replies:
    2
    Views:
    2,425
  4. Replies:
    5
    Views:
    2,578
    Luc The Perverse
    Aug 14, 2006
  5. chirs
    Replies:
    3
    Views:
    183
    Dr John Stockton
    Dec 1, 2003
Loading...

Share This Page