Ruby Unpack and Pack

Unpack is a pretty cool ruby method. The most useful part of Unpack is that it takes an item and returns you the binary sequence of that object, it can also return the item to you in hexadecimal and you can see a chart of other arguments it takes here.

How it unpacks:

The ASCII value of G is 71 unpack gives us this:

By passing unpack (B*) we are asking it to give us the binary value of the ASCII value:

There are two ways of displaying the binary value MSB(Most significant bit) and LSB(least significant bit) Not exactly sure why you would want to do this but Ruby does give us this option here is “G” in LSB:

You can tell that its the exact same thing as MSB but backwards.

And there is also pack which does the opposite of unpack, one way to use pack is to convert arrays into hexadecimals.

Hexadecimal is just a combination of numbers (0–9) and (a-f) that are used to represent numbers. Here is an example using pack:

Here is a video of someone who among other things used pack to write sinatra in 6 lines.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.