Unpacking Strings Into Binary
You can find the binary representation of a given string by decoding it. Ruby comes equipped with the #unpack
method on the String
class that can do this decoding.
Though there are a variety of formats to decode a string into, here are some example of decoding different characters into binary.
The B*
says unpack this into as many Binary digits as are needed. The UTF-8 encoding, means only a single byte (8-bits) are needed to represent "A"
.
"Æ"
is represented by two bytes. We can unpack each byte seprarately using "B8 B8"
.
Similarly, this Japanese character is represented by three bytes of data.
Lastly, emojis generally require four bytes of data.
Last updated