Convert String to BinaryString

Hi guys,

Once I already asked how to create a hex string in â??little-endianâ? order
in Ruby. In this case the most appropriate way is to use function
sprintf() or “%”. But
those function return aString but I need aBinarystring. So, my question
is how to convert aString to aBinarystring?

Thanks in advance.

On Jun 21, 2006, at 2:13 PM, Michael stepanov wrote:

Thanks in advance.


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

% irb
irb(main):001:0> 12.to_s(2)
=> “1100”
irb(main):002:0> “12”.to_i.to_s(2)
=> “1100”

Logan C. wrote:

On Jun 21, 2006, at 2:13 PM, Michael stepanov wrote:

Thanks in advance.


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

% irb
irb(main):001:0> 12.to_s(2)
=> “1100”
irb(main):002:0> “12”.to_i.to_s(2)
=> “1100”

I found how to send what I need with pack:

s = [1,3,144,3,50].pack(“NnNNe”)
print s.unpack(“H*”)[0] => 000000010003000000720000000342480000

pack is cool, but Template characters I found here -
http://www.rubycentral.com/ref/ref_c_array.html#include_qm

Michael stepanov wrote:

pack is cool, but Template characters I found here -
http://www.rubycentral.com/ref/ref_c_array.html#include_qm

You can also use

ri pack
ri unpack

for a quick reference.

Joel VanderWerf wrote:

Michael stepanov wrote:

pack is cool, but Template characters I found here -
http://www.rubycentral.com/ref/ref_c_array.html#include_qm

You can also use

ri pack
ri unpack

for a quick reference.

Thanks a lot! It’s very useful!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs