Bin2str conversion

Hello,

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:

str.unpack('c*').collect { |x| sprintf('%02x', x) }.to_s.hex.to_s(2)

As you can see, this line can convert the string *str* to its binary
format.

Thanks for your help.

Zhang'

···

--
Posted via http://www.ruby-forum.com/.

Just so you know, at least on 1.8.6, this code will only return the
binary for one character, the first one.

Have you looked at Array#pack? That might be what you look for.

Todd

···

On Sat, Mar 8, 2008 at 7:11 PM, Zangief Ief <z4n9ief@gmail.com> wrote:

Hello,

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:

str.unpack('c*').collect { |x| sprintf('%02x', x) }.to_s.hex.to_s(2)

Hi,

···

On Sun, Mar 9, 2008 at 12:11 PM, Zangief Ief <z4n9ief@gmail.com> wrote:

I would like to write a function which is able to convert a binary data
into its string format...

So, for example, do you want to be able to take some string, and turn it
into something that looks like "98892963bf862bdeb6af019fce", and then back
again? I'm just trying to work out your meaning of binary data/string
format!

Arlen

Hi,

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.

···

On Sun, Mar 9, 2008 at 12:11 PM, Zangief Ief <z4n9ief@gmail.com> wrote:

Todd Benson wrote:

···

On Sat, Mar 8, 2008 at 7:11 PM, Zangief Ief <z4n9ief@gmail.com> wrote:

Have you looked at Array#pack? That might be what you look for.

Actually I think a good way to do it would be to use the Array#pack
method. But after some tests, I haven't do it successfully.
--
Posted via http://www.ruby-forum.com/\.

Thank you Arlen Cuss, yes it's what I would like to found :slight_smile:
But it seems there is a problem when we try to convert some special char
like "·.‘Nú∆Ï"

Macintosh:~ zhang$ ruby /Users/cyril/Desktop/test.rb
0-111110-10010010010111000-11110-10000000-1101000010011100-111101-100011000-11110-1111000-11110100-111101-1110001
?.?'

As you can see, there is some '-' char in the binary result... O_o

···

--
Posted via http://www.ruby-forum.com/.

Hi,

> 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

Not sure, but that ^^^^ line seems to be the same as this...

unpack('B*').first

  end

  def self.from_binary b
    b.scan(/.{8}/).map {|x| x.to_i(2)}.pack('C*')

I thought I could come up with something better than this, but I guess
not right now :slight_smile:

  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.

Todd

···

On Sun, Mar 9, 2008 at 4:41 AM, Arlen Cuss <celtic@sairyx.org> wrote:

On Sun, Mar 9, 2008 at 12:11 PM, Zangief Ief <z4n9ief@gmail.com> wrote:

Hi Zhang,

But it seems there is a problem when we try to convert some special char
like "·.'Nú∆Ï"

You're right! My apologies! If we add the statement `p x' in the to_binary
map, we see this:
-62
-73
46
-30
-128
...

Actually, this is due to unpack('c*') - `c' extracts a character as an
integer, and on the base system, that means from -128 to 127, or so.

Change unpack('c*') to unpack('C*'), *and* pack('c*') to pack('C*'). This
extracts them as unsigned integers, 0 to 255, like we expect. :slight_smile: I get the
correct result after doing that.

Cheers!
Arlen

···

On Sun, Mar 9, 2008 at 10:34 PM, Zangief Ief <z4n9ief@gmail.com> wrote:

Thanks :slight_smile:
Your answer was been very usefull. Simple and clear.

···

--
Posted via http://www.ruby-forum.com/.