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’

On Sat, Mar 8, 2008 at 7:11 PM, Zangief I. [email protected] 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)

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

Todd B. wrote:

On Sat, Mar 8, 2008 at 7:11 PM, Zangief I. [email protected] 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.

Hi,

On Sun, Mar 9, 2008 at 12:11 PM, Zangief I. [email protected] 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,

On Sun, Mar 9, 2008 at 12:11 PM, Zangief I. [email protected] 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?

[email protected]:~$ 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)

[email protected]:~$ ruby test.rb
01001000011001010110110001101100011011110010110000100000011101110110111101110010011011000110010000100001
Hello, world!
[email protected]:~$

HTH,
Arlen.

Thank you Arlen C., 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

Hi Zhang,

On Sun, Mar 9, 2008 at 10:34 PM, Zangief I. [email protected] wrote:

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 4:41 AM, Arlen C. [email protected] wrote:

My apologies, I just got what you meant! How’s this?

[email protected]:~$ 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:

[email protected]:~$

HTH,
Arlen.

Todd

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