# Bin2str conversion

**URL:** <https://www.ruby-forum.com/t/bin2str-conversion/131382>\
**Category:** Ruby\
**Created:** [March 9, 2008, 2:11am UTC](https://www.ruby-forum.com/t/bin2str-conversion/131382 "2008-03-09T02:11:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 9, 2008, 2:11am UTC](https://www.ruby-forum.com/t/bin2str-conversion/131382/1 "2008-03-09T02:11:36Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 9, 2008, 3:23am UTC](https://www.ruby-forum.com/t/bin2str-conversion/131382/2 "2008-03-09T03:23:02Z")

</div>

On Sat, Mar 8, 2008 at 7:11 PM, Zangief I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 9, 2008, 10:00am UTC](https://www.ruby-forum.com/t/bin2str-conversion/131382/3 "2008-03-09T10:00:53Z")

</div>

Todd B. wrote:

> On Sat, Mar 8, 2008 at 7:11 PM, Zangief I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 9, 2008, 10:41am UTC](https://www.ruby-forum.com/t/bin2str-conversion/131382/4 "2008-03-09T10:41:03Z")

</div>

Hi,

On Sun, Mar 9, 2008 at 12:11 PM, Zangief I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 9, 2008, 10:42am UTC](https://www.ruby-forum.com/t/bin2str-conversion/131382/5 "2008-03-09T10:42:50Z")

</div>

Hi,

On Sun, Mar 9, 2008 at 12:11 PM, Zangief I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 9, 2008, 12:34pm UTC](https://www.ruby-forum.com/t/bin2str-conversion/131382/6 "2008-03-09T12:34:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 9, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/bin2str-conversion/131382/7 "2008-03-09T13:41:48Z")

</div>

Hi Zhang,

On Sun, Mar 9, 2008 at 10:34 PM, Zangief I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 🙂 I get  
the  
correct result after doing that.

Cheers!  
Arlen

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 9, 2008, 9:38pm UTC](https://www.ruby-forum.com/t/bin2str-conversion/131382/8 "2008-03-09T21:38:25Z")

</div>

On Sun, Mar 9, 2008 at 4:41 AM, Arlen C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 🙂

> celtic@sohma:~$
> 
> HTH,  
> Arlen.

Todd

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 9, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/bin2str-conversion/131382/9 "2008-03-09T22:28:14Z")

</div>

Thanks 🙂  
Your answer was been very usefull. Simple and clear.
