convert ASCII to byte an vice versa

Discussion in 'Ruby' started by chen li, Jul 13, 2007.

  1. chen li

    chen li Guest

    Hi all,

    I have a small script as follows. I wonder how I can
    change 65 66 67 back to ABC.

    Thanks,

    Li


    C:\Documents and Settings\chen73>irb
    irb(main):001:0> str="ABC"
    => "ABC"
    irb(main):002:0> str.each_byte{|b|puts b}
    65
    66
    67
    => "ABC"
    irb(main):003:0>



    ____________________________________________________________________________________
    Choose the right car based on your needs. Check out Yahoo! Autos new Car Finder tool.
    http://autos.yahoo.com/carfinder/
    chen li, Jul 13, 2007
    #1
    1. Advertising

  2. chen li

    Val Guest

    On Jul 13, 9:05 am, chen li <> wrote:
    > Hi all,
    >
    > I have a small script as follows. I wonder how I can
    > change 65 66 67 back to ABC.
    >
    > Thanks,
    >
    > Li
    >
    > C:\Documents and Settings\chen73>irb
    > irb(main):001:0> str="ABC"
    > => "ABC"
    > irb(main):002:0> str.each_byte{|b|puts b}
    > 65
    > 66
    > 67
    > => "ABC"
    > irb(main):003:0>
    >
    > ____________________________________________________________________________________
    > Choose the right car based on your needs. Check out Yahoo! Autos new Car Finder tool.http://autos.yahoo.com/carfinder/




    # [65,66,67].collect{|b| b.chr}.join
    => "ABC"
    # [65,66,67].pack("C*")
    => "ABC"

    Val

    http://revolutiononrails.blogspot.com/
    Val, Jul 13, 2007
    #2
    1. Advertising

  3. On Jul 13, 9:05 am, chen li <> wrote:
    > Hi all,
    >
    > I have a small script as follows. I wonder how I can
    > change 65 66 67 back to ABC.
    >
    >
    > C:\Documents and Settings\chen73>irb
    > irb(main):001:0> str="ABC"
    > => "ABC"
    > irb(main):002:0> str.each_byte{|b|puts b}



    irb(main):003:0> str.each_byte{|b|puts b.chr}
    A
    B
    C
    => "ABC"
    Karl von Laudermann, Jul 13, 2007
    #3
  4. chen li <> writes:

    > Hi all,
    >
    > I have a small script as follows. I wonder how I can
    > change 65 66 67 back to ABC.


    In addition to manually using Integer.chr, you may find Array.pack
    useful:

    esau:~$ irb
    irb(main):001:0> 'asdf'.unpack('c*')
    => [97, 115, 100, 102]
    irb(main):002:0> [97, 115, 100, 102].pack('c*')
    => "asdf"


    --
    s=%q( Daniel Martin --
    puts "s=%q(#{s})",s.to_a.last )
    puts "s=%q(#{s})",s.to_a.last
    Daniel Martin, Jul 13, 2007
    #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. carmen
    Replies:
    4
    Views:
    30,708
    ersecchio
    Jan 12, 2010
  2. -
    Replies:
    8
    Views:
    614
    Antti S. Brax
    Jun 11, 2005
  3. Flyingaway
    Replies:
    6
    Views:
    22,796
    Jack Klein
    Feb 19, 2005
  4. Byron
    Replies:
    6
    Views:
    499
    Byron
    Sep 16, 2004
  5. Eugene Anthony

    Byte string to Unicode & vice versa

    Eugene Anthony, Apr 27, 2006, in forum: ASP General
    Replies:
    1
    Views:
    158
    Eugene Anthony
    Apr 28, 2006
Loading...

Share This Page