|
| 1 | +# Unpacking Strings Into Binary |
| 2 | + |
| 3 | +You can find the binary representation of a given string by decoding it. Ruby |
| 4 | +comes equipped with the [`#unpack`](https://apidock.com/ruby/String/unpack) |
| 5 | +method on the `String` class that can do this decoding. |
| 6 | + |
| 7 | +Though there are a variety of formats to decode a string into, here are some |
| 8 | +example of decoding different characters into binary. |
| 9 | + |
| 10 | +```ruby |
| 11 | +> "A".unpack("B*") |
| 12 | +=> ["01000001"] |
| 13 | +``` |
| 14 | + |
| 15 | +The `B*` says _unpack_ this into as many *B*inary digits as are needed. The |
| 16 | +UTF-8 encoding, means only a single byte (8-bits) are needed to represent |
| 17 | +`"A"`. |
| 18 | + |
| 19 | +```ruby |
| 20 | +irb(main):002:0> "Æ".unpack("B*") |
| 21 | +=> ["1100001110000110"] |
| 22 | +irb(main):003:0> "Æ".unpack("B8 B8") |
| 23 | +=> ["11000011", "10000110"] |
| 24 | +``` |
| 25 | + |
| 26 | +`"Æ"` is represented by two bytes. We can unpack each byte seprarately using |
| 27 | +`"B8 B8"`. |
| 28 | + |
| 29 | +```ruby |
| 30 | +irb(main):004:0> "木".unpack("B*") |
| 31 | +=> ["111001101001110010101000"] |
| 32 | +irb(main):005:0> "木".unpack("B8 B8 B8") |
| 33 | +=> ["11100110", "10011100", "10101000"] |
| 34 | +``` |
| 35 | + |
| 36 | +Similarly, this Japanese character is represented by three bytes of data. |
| 37 | + |
| 38 | +```ruby |
| 39 | +irb(main):006:0> "👻".unpack("B*") |
| 40 | +=> ["11110000100111111001000110111011"] |
| 41 | +irb(main):007:0> "👻".unpack("B8 B8 B8 B8") |
| 42 | +=> ["11110000", "10011111", "10010001", "10111011"] |
| 43 | +``` |
| 44 | + |
| 45 | +Lastly, emojis generally require four bytes of data. |
| 46 | + |
| 47 | +[source](https://www.honeybadger.io/blog/the-rubyist-guide-to-unicode-utf8/) |
0 commit comments