bin2str conversion

Z

Zangief Ief

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'
 
T

Todd Benson

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
 
Z

Zangief Ief

Todd said:
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.
 
A

Arlen Cuss

[Note: parts of this message were removed to make it a legal post.]

Hi,

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
 
A

Arlen Cuss

[Note: parts of this message were removed to make it a legal post.]

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.
 
Z

Zangief Ief

Thank you Arlen Cuss, yes it's what I would like to found :)
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
 
A

Arlen Cuss

SGkgWmhhbmcsCgpPbiBTdW4sIE1hciA5LCAyMDA4IGF0IDEwOjM0IFBNLCBaYW5naWVmIEllZiA8
ejRuOWllZkBnbWFpbC5jb20+IHdyb3RlOgoKPiBCdXQgaXQgc2VlbXMgdGhlcmUgaXMgYSBwcm9i
bGVtIHdoZW4gd2UgdHJ5IHRvIGNvbnZlcnQgc29tZSBzcGVjaWFsIGNoYXIKPiBsaWtlICLCty4n
TsO64oiGw48iCgoKWW91J3JlIHJpZ2h0ISBNeSBhcG9sb2dpZXMhIElmIHdlIGFkZCB0aGUgc3Rh
dGVtZW50IGBwIHgnIGluIHRoZSB0b19iaW5hcnkKbWFwLCB3ZSBzZWUgdGhpczoKLTYyCi03Mwo0
NgotMzAKLTEyOAouLi4KCkFjdHVhbGx5LCB0aGlzIGlzIGR1ZSB0byB1bnBhY2soJ2MqJykgLSBg
YycgZXh0cmFjdHMgYSBjaGFyYWN0ZXIgYXMgYW4KaW50ZWdlciwgYW5kIG9uIHRoZSBiYXNlIHN5
c3RlbSwgdGhhdCBtZWFucyBmcm9tIC0xMjggdG8gMTI3LCBvciBzby4KCkNoYW5nZSB1bnBhY2so
J2MqJykgdG8gdW5wYWNrKCdDKicpLCAqYW5kKiBwYWNrKCdjKicpIHRvIHBhY2soJ0MqJykuIFRo
aXMKZXh0cmFjdHMgdGhlbSBhcyB1bnNpZ25lZCBpbnRlZ2VycywgMCB0byAyNTUsIGxpa2Ugd2Ug
ZXhwZWN0LiA6KSBJIGdldCB0aGUKY29ycmVjdCByZXN1bHQgYWZ0ZXIgZG9pbmcgdGhhdC4KCkNo
ZWVycyEKQXJsZW4K
 
T

Todd Benson

Hi,




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 :)
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
 

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
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top