dump the content of a string

Discussion in 'Perl Misc' started by toylet, Feb 25, 2004.

  1. toylet

    toylet Guest

    my $line=<>;

    I input 'a' and press enter.

    $line should now be 2 bytes in length.

    1. How could I access the 2 bytes respectively
    2. and print them in hex format?

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 9:04pm up 2:47 1 user 1.12 1.04
     
    toylet, Feb 25, 2004
    #1
    1. Advertising

  2. toylet

    Web Surfer Guest

    [This followup was posted to comp.lang.perl.misc]

    In article <403c9dd8$-cable.com>, toylet
    <toylet_at_mail.hongkong.com> says...
    >
    > my $line=<>;
    >
    > I input 'a' and press enter.
    >
    > $line should now be 2 bytes in length.
    >
    > 1. How could I access the 2 bytes respectively


    You can use "substr" to extract portions of strings


    > 2. and print them in hex format?



    printf "%02x\n",$data; # print data in hex
     
    Web Surfer, Feb 25, 2004
    #2
    1. Advertising

  3. toylet wrote:
    > my $line=<>;
    >
    > I input 'a' and press enter.
    >
    > $line should now be 2 bytes in length.


    Not necessarily. It depends on your OS.
    If you want to write portable code don't expect the input to be just 2
    characters.

    > 1. How could I access the 2 bytes respectively


    perldoc -f substr

    > 2. and print them in hex format?


    perldoc -f ord
    perldoc -f hex

    jue
     
    Jürgen Exner, Feb 25, 2004
    #3
  4. toylet

    Uri Guttman Guest

    >>>>> "JE" == Jürgen Exner <> writes:

    >> 2. and print them in hex format?


    JE> perldoc -f ord
    JE> perldoc -f hex

    hex converts from ascii hex to binary. it doesn't print hex.

    sprintf or unpack are two ways to do it.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 25, 2004
    #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:
    2
    Views:
    1,846
  2. hazz
    Replies:
    6
    Views:
    50,019
    SkyUCHC
    Jun 9, 2010
  3. halfdog
    Replies:
    12
    Views:
    12,583
  4. Jammer

    string byte dump

    Jammer, Jan 28, 2007, in forum: Python
    Replies:
    3
    Views:
    292
    John Machin
    Jan 28, 2007
  5. Jake
    Replies:
    1
    Views:
    328
    mlimber
    Nov 11, 2005
Loading...

Share This Page