convert ASCII to byte an vice versa

C

chen li

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/
 
V

Val

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/
 
K

Karl von Laudermann

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"
 
D

Daniel Martin

chen li said:
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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top