Convert ASCII to byte an vice versa

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>

On 7/13/07, chen li [email protected] wrote:

Hi all,

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

Try the Integer#chr method, from the docs:

int.chr => string
http://www.ruby-doc.org/core/classes/Integer.src/M001163.html

Returns a string containing the ASCII character represented by the
receiver’s value.

65.chr #=> “A”
?a.chr #=> “a”
230.chr #=> “\346”

On Jul 13, 9:05 am, chen li [email protected] wrote:

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/

On Jul 13, 9:05 am, chen li [email protected] 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”

chen li [email protected] 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”