From: Arlen Cuss Date: 2008-03-09T18:41:47+09:00 Subject: Re: bin2str conversion ------=_Part_43646_27163649.1205055716626 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, On Sun, Mar 9, 2008 at 12:11 PM, Zangief Ief wrote: > I would like to write a function which is able to convert a binary data > into its string format... > For example, here I have the opposite of my goal. My apologies, I just got what you meant! How's this? celtic@sohma:~$ cat test.rb class String def to_binary self.unpack('c*').map {|x| x.to_s(2).rjust(8, '0')}.join end def self.from_binary b b.scan(/.{8}/).map {|x| x.to_i(2)}.pack('c*') end end string = "Hello, world!" puts binary = string.to_binary puts String.from_binary(binary) celtic@sohma:~$ ruby test.rb 01001000011001010110110001101100011011110010110000100000011101110110111101110010011011000110010000100001 Hello, world! celtic@sohma:~$ HTH, Arlen. ------=_Part_43646_27163649.1205055716626--