How Does Ruby handle bytes/binary?

14,548

Solution 1

To make a string that has an arbitrary sequence of bytes, do something like this:

binary_string = "\xE5\xA5\xBD"

The "\x" is a special escape to encode an arbitrary byte from hex, so "\xE5" means byte 0xE5.

Then try sending that string on the socket.

Solution 2

Don't know if this helps enough, but you can index the bits in an integer in ruby.

n = 0b010101

n # => 21

n = 21

n[0]  # => 1
n[1]  # => 0
n[2]  # => 1
n[3]  # => 0
n[4]  # => 1
n[5]  # => 0

Solution 3

Have a look at the String.unpack method. This is an example:

str = "1010"
str.unpack("cccc")
=> [49, 48, 49, 48]

This will give you integer values. There are more ways to do the conversion.

Share:
14,548

Related videos on Youtube

Anikeev Gennadiy
Author by

Anikeev Gennadiy

I have to deal with a wide variety of different technologies, often ones I have no context for understanding, so I tend to post here a lot about a lot of different topics (Rails, Blackberry/Android development, SQL, OpenLayers, and Raphael are a few examples).

Updated on November 21, 2020

Comments

  • Anikeev Gennadiy
    Anikeev Gennadiy over 3 years

    I'm trying to send a series of binary bytes across a socket, to meet a particular standard my company uses. No one in my company has used Ruby for this before, but in other languages, they send the data across one byte at a time, (usually with some sort of "pack" method).

    I can't find anyway to create binary on the fly, or create bytes at all (the closest I can find it how you can turn a string into the bytes representing it's characters).

    I know you can say something like :

    @var = 0b101010101

    But how would I convert a string in the form "101010101" or the resulting integer created when I do string.to_i(2) into an actual binary. If I just send the string accross a socket, won't that just send the ASCII for "0" and "1" instead of the literal characters?

    Surely there is SOME way to do this natively in Ruby?

  • Anikeev Gennadiy
    Anikeev Gennadiy almost 15 years
    Before this post was edited, it told me about Array.pack. I don't quite understand this. If I have: @bob = ["111111111", "1000", "1111"] or whatever, and then I say: @bob.unpack(b*), it displays something like: "/377" Is that the correct binary? Isn't that only the first element of the array? How would I have it do the entire array?
  • NikolaDjokic
    NikolaDjokic almost 15 years
    Array.pack is the opposite of String.unpack. In your example bob.pack('b*') is only packing first element of array. You probably want bob.pack('bbb*'). However this is something different from what you are asking for in the question.
  • why
    why over 12 years
    Why there are some "/" in binary_string ?
  • David Grayson
    David Grayson over 12 years
    It's quite possible for a binary string to have a byte with value 0x2F (47). That's the ASCII encoding of "/" so if you inspect the string Ruby will usually print out a "/". The same thing goes for any ASCII character. Ruby doesn't actually know if your string is a "binary string", to Ruby your string is basically a sequence of bytes (except in 1.9 they added some encoding information).
  • why
    why over 12 years
    Thanks! it helps me very much