Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > convert ASCII to byte an vice versa

Reply
Thread Tools

convert ASCII to byte an vice versa

 
 
chen li
Guest
Posts: n/a
 
      07-13-2007
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/

 
Reply With Quote
 
 
 
 
Val
Guest
Posts: n/a
 
      07-13-2007
On Jul 13, 9:05 am, chen li <(E-Mail Removed)> 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/

 
Reply With Quote
 
 
 
 
Karl von Laudermann
Guest
Posts: n/a
 
      07-13-2007
On Jul 13, 9:05 am, chen li <(E-Mail Removed)> 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"


 
Reply With Quote
 
Daniel Martin
Guest
Posts: n/a
 
      07-13-2007
chen li <(E-Mail Removed)> 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 -- http://www.velocityreviews.com/forums/(E-Mail Removed)
puts "s=%q(#{s})",s.to_a.last )
puts "s=%q(#{s})",s.to_a.last

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting int and short to byte array and vice versa carmen Java 4 01-12-2010 05:00 PM
Byte string to Unicode & vice versa Eugene Anthony ASP General 1 04-28-2006 09:57 AM
How to convert '1' into '-1' and vice versa? - Java 8 06-11-2005 09:28 AM
Why convert Word doc to PDF? Or vice versa Harry Computer Support 3 03-08-2005 12:44 PM
Convert a string to float and vice versa Flyingaway C++ 6 02-19-2005 11:08 PM



Advertisments