Skip to content

Commit 4be84f8

Browse files
committed
Add Unpacking Strings Into Binary as a ruby til
1 parent f61df4b commit 4be84f8

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
99

1010
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1111

12-
_899 TILs and counting..._
12+
_900 TILs and counting..._
1313

1414
---
1515

@@ -744,6 +744,7 @@ _899 TILs and counting..._
744744
- [Turning Things Into Hashes](ruby/turning-things-into-hashes.md)
745745
- [Uncaught Exceptions In Pry](ruby/uncaught-exceptions-in-pry.md)
746746
- [`undef_method` And The Inheritance Hierarchy](ruby/undef-method-and-the-inheritance-hierarchy.md)
747+
- [Unpacking Strings Into Binary](ruby/unpacking-strings-into-binary.md)
747748
- [Up And Down With Integers](ruby/up-and-down-with-integers.md)
748749
- [Use A Case Statement As A Cond Statement](ruby/use-a-case-statement-as-a-cond-statement.md)
749750
- [Use dotenv In A Non-Rails Project](ruby/use-dotenv-in-a-non-rails-project.md)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

Comments
 (0)