Converting byte to integer string

Discussion in 'Perl Misc' started by kerry.baker@orcon.net.nz, Apr 3, 2006.

  1. Guest

    I have a strange problem. I have a need to convert the value of a byte
    read from a file to its integer string counterpart. (e.g. the byte
    contains 0x1d and it prints out 29.)
    I use this to do it:
    $value = unpack ("c",substr($_, 259, 1));

    This seems to work just fine for all values except 0x0a which returns
    32 instead of 10.
    Why does this happen?
    I wrote a similar script using awk which also returned 32 instead of
    10.
    It seems so odd to me. 0x0b returns 11 which is correct as does all
    other values.
    Any ideas greatfully accepted!
    , Apr 3, 2006
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > I have a strange problem. I have a need to convert the value of a byte
    > read from a file to its integer string counterpart. (e.g. the byte
    > contains 0x1d and it prints out 29.)
    > I use this to do it:
    > $value = unpack ("c",substr($_, 259, 1));
    >
    > This seems to work just fine for all values except 0x0a which returns
    > 32 instead of 10.
    > Why does this happen?
    > I wrote a similar script using awk which also returned 32 instead of
    > 10.
    > It seems so odd to me. 0x0b returns 11 which is correct as does all
    > other values.


    perl -le 'print unpack "c", chr 0x0a'
    10

    Have you checked the character you're accessing?

    Anno

    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Apr 3, 2006
    #2
    1. Advertising

  3. (s)printf sort of works:

    perl -e '{printf(%c,'0x0a');print"\n";}'
    10
    > perl -e '{printf(%c,'0x0b');print"\n";}'

    11
    > perl -e '{printf(%c,'0x0c');print"\n";}'

    12
    > perl -e '{printf("%c",'0x0c');print"\n";}'



    >
    > perl -e '{$val = '0x0b';printf(%c, $val);print"\n";}'

    11


    quoting the format screws it up.

    Chris
    --
    Chris Richmond | I don't speak for Intel & vise versa
    Chris Richmond - MD6-FDC ~, Apr 4, 2006
    #3
    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. Peter
    Replies:
    3
    Views:
    716
    Michael Borgwardt
    Aug 5, 2004
  2. Kirby
    Replies:
    3
    Views:
    631
    Kirby
    Oct 8, 2004
  3. Polaris431
    Replies:
    8
    Views:
    740
    SM Ryan
    Dec 4, 2006
  4. Randy Kramer
    Replies:
    12
    Views:
    360
    Robert Klemme
    Oct 25, 2007
  5. Shashank Khanvilkar

    Converting a 4 byte field to integer.

    Shashank Khanvilkar, Jul 29, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    113
    Sherm Pendley
    Jul 29, 2005
Loading...

Share This Page