Converting binary data to string in ruby

You can do so via using base64 which is a fairly universal way.

require 'base64'

str = Base64.encode64(data)

Another way to play with binary data is String#unpack.


if u have a binary string lets say something like:

s = "01001101011011110111000101110101011001010110010101110100"

and u wanna convert it back to ascii text in Ruby u can do like:

s = "01001101011011110111000101110101011001010110010101110100"

(0..s.length-8).step(8) do |i|
    print s[i,8].to_i(base=2).chr
end

Hope this will help someone :)

Tags:

Ruby