dump the real string

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

  1. toylet

    toylet Guest

    my $line = <>;

    I input "a" and press enter.

    Now $line should contain 2 bytes: 'a' and end-of-line character.

    How could I print the content of $line (both bytes) in hex format?


    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 8:58pm up 2:41 1 user 0.99 0.98
     
    toylet, Feb 25, 2004
    #1
    1. Advertising

  2. "toylet" <toylet_at_mail.hongkong.com> wrote in message
    news:-cable.com...
    >
    > my $line = <>;
    >
    > I input "a" and press enter.
    >
    > Now $line should contain 2 bytes: 'a' and end-of-line character.
    >
    > How could I print the content of $line (both bytes) in hex format?
    >


    perldoc -f substr
    perldoc -f hex
     
    Andrew McGregor, Feb 25, 2004
    #2
    1. Advertising

  3. toylet

    toylet Guest

    >> my $line = <>;
    >>
    >> I input "a" and press enter.
    >>
    >> Now $line should contain 2 bytes: 'a' and end-of-line character.
    >>
    >> How could I print the content of $line (both bytes) in hex format?

    > perldoc -f substr
    > perldoc -f hex


    hex() should not be relevant as I need to convert from numeric to hex
    digits. Already knew about substr().




    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 9:30pm up 3:13 1 user 1.03 1.00
     
    toylet, Feb 25, 2004
    #3
  4. toylet

    toylet Guest

    >> my $line = <>;
    > perldoc -f substr
    > perldoc -f hex


    I tried this:
    printf(".%x.\n.%x.\n",substr($line,0,1),substr($line,1,1));

    and perl said substr($line) are not numeric.

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 9:34pm up 3:17 1 user 1.11 1.04
     
    toylet, Feb 25, 2004
    #4
  5. toylet

    toylet Guest

    >> my $line = <>;
    >> I input "a" and press enter.


    and which function will return the length of $line as 2 bytes?
    $line is already in scalar context.

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 9:38pm up 3:21 1 user 1.04 1.01
     
    toylet, Feb 25, 2004
    #5
  6. toylet

    toylet Guest

    toylet wrote:
    >>> my $line = <>;

    >> perldoc -f substr
    >> perldoc -f hex

    >
    > I tried this:
    > printf(".%x.\n.%x.\n",substr($line,0,1),substr($line,1,1));
    >
    > and perl said substr($line) are not numeric.
    >

    got it... they got the c and foxpro function chr() from the doc, I saw
    ord().

    my $line=<>
    printf(".%x.\n.%x.\n",ord(substr($line,0,1)),ord(substr($line,1,1)));


    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 9:40pm up 3:23 1 user 1.04 1.01
     
    toylet, Feb 25, 2004
    #6
  7. toylet

    toylet Guest

    toylet wrote:

    >>> my $line = <>;
    >>> I input "a" and press enter.

    >
    > and which function will return the length of $line as 2 bytes?
    > $line is already in scalar context.
    >

    OK, I think it should be length().

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 9:42pm up 3:25 1 user 1.05 1.01
     
    toylet, Feb 25, 2004
    #7
  8. toylet

    toylet Guest

    >> Already knew about substr().
    > Then why did you ask?


    Not sure whether perl store strings like C.


    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 10:08pm up 3:51 1 user 1.13 1.10
     
    toylet, Feb 25, 2004
    #8
  9. toylet <> wrote:
    > my $line=<>
    > printf(".%x.\n.%x.\n",ord(substr($line,0,1)),ord(substr($line,1,1)));


    Or, not assuming the length of $line is 2:

    printf ".%02x.\n", ord foreach (split //, $line);


    --
    Glenn Jackman
    NCF Sysadmin
     
    Glenn Jackman, Feb 25, 2004
    #9
  10. toylet wrote:
    > my $line = <>;
    >
    > I input "a" and press enter.
    >
    > Now $line should contain 2 bytes: 'a' and end-of-line character.
    >
    > How could I print the content of $line (both bytes) in hex format?


    Why are you asking the same question twice within 3 minutes?

    jue
     
    Jürgen Exner, Feb 25, 2004
    #10
  11. toylet wrote:
    > toylet wrote:
    >
    >>>> my $line = <>;
    >>>> I input "a" and press enter.

    >>
    >> and which function will return the length of $line as 2 bytes?
    >> $line is already in scalar context.
    >>

    > OK, I think it should be length().


    That's wrong. length() will return the number of characters in the string.
    This is totally different from the length of the string in bytes.

    If you want the length in bytes then probably you will have to dig very deep
    into the perl implementation. But I wonder why you would want to know this
    implementation detail?

    jue
     
    Jürgen Exner, Feb 25, 2004
    #11
  12. toylet

    gnari Guest

    "toylet" <toylet_at_mail.hongkong.com> wrote in message
    news:-cable.com...
    >
    >
    > Now $line should contain 2 bytes: 'a' and end-of-line character.
    >
    > How could I print the content of $line (both bytes) in hex format?


    take a look at unpack() and his brother pack()

    gnari
     
    gnari, Feb 25, 2004
    #12
  13. toylet

    Jay Tilton Guest

    toylet <toylet_at_mail.hongkong.com> wrote:

    : toylet wrote:
    : >>> my $line = <>;
    : >> perldoc -f substr
    : >> perldoc -f hex
    : >
    : > I tried this:
    : > printf(".%x.\n.%x.\n",substr($line,0,1),substr($line,1,1));
    : >
    : > and perl said substr($line) are not numeric.
    : >
    : got it... they got the c and foxpro function chr() from the doc, I saw
    : ord().
    :
    : my $line=<>
    : printf(".%x.\n.%x.\n",ord(substr($line,0,1)),ord(substr($line,1,1)));

    Yuck.

    printf "%vx\n", $line;
     
    Jay Tilton, Feb 25, 2004
    #13
  14. toylet

    toylet Guest

    > : my $line=<>
    > : printf(".%x.\n.%x.\n",ord(substr($line,0,1)),ord(substr($line,1,1)));
    > Yuck.
    > printf "%vx\n", $line;


    That's really neat.

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 9:02am up 14:45 1 user 1.02 1.00
     
    toylet, Feb 26, 2004
    #14
  15. toylet

    toylet Guest

    > Or, not assuming the length of $line is 2:
    >
    > printf ".%02x.\n", ord foreach (split //, $line);


    can I also use split(/\b/,$line)?

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 9:02am up 14:45 1 user 1.02 1.00
     
    toylet, Feb 26, 2004
    #15
  16. toylet

    toylet Guest

    > That's wrong. length() will return the number of characters in the string.
    > This is totally different from the length of the string in bytes.


    You emant each char in a perl string is not stored as one byte?

    > If you want the length in bytes then probably you will have to dig very deep
    > into the perl implementation. But I wonder why you would want to know this
    > implementation detail?


    Sorry, I possibly asked the wrong question. I don't really need that deep.


    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 9:04am up 14:47 1 user 1.01 1.00
     
    toylet, Feb 26, 2004
    #16
  17. toylet

    toylet Guest

    > Why are you asking the same question twice within 3 minutes?

    input error. :)

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 9:04am up 14:47 1 user 1.01 1.00
     
    toylet, Feb 26, 2004
    #17
  18. toylet <> wrote:
    >> Or, not assuming the length of $line is 2:
    >>
    >> printf ".%02x.\n", ord foreach (split //, $line);

    >
    > can I also use split(/\b/,$line)?



    What happened when you tried it?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 26, 2004
    #18
  19. toylet <> wrote:
    >> : my $line=<>
    >> : printf(".%x.\n.%x.\n",ord(substr($line,0,1)),ord(substr($line,1,1)));
    >> Yuck.
    >> printf "%vx\n", $line;

    >
    > That's really neat.



    I wonder who wrote it...


    [ Please learn to properly quote followups.
    Soon.
    ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 26, 2004
    #19
  20. toylet <> wrote:
    >> That's wrong. length() will return the number of characters in the string.
    >> This is totally different from the length of the string in bytes.

    >
    > You emant each char in a perl string is not stored as one byte?



    You mean ASCII doesn't work for Chinese?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 26, 2004
    #20
    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. senthil
    Replies:
    5
    Views:
    1,389
    senthil
    Jan 24, 2004
  2. Replies:
    2
    Views:
    1,795
  3. halfdog
    Replies:
    12
    Views:
    12,454
  4. Alexander Eisenhuth
    Replies:
    5
    Views:
    540
    Bob Gailer
    Jul 25, 2003
  5. Curious Trigger
    Replies:
    2
    Views:
    1,815
    Curious Trigger
    Sep 9, 2006
Loading...

Share This Page