UTF-8, Codepoint, Bitstring, Binaries, Charlist in elixir
/ 2 min read
I wanted to have a clear understanding of what are UTF-8 Codepoints, Bitstring, Binaries and Charlists so i write a long the way i learn.
What are Strings?
Strings are list of integers which are valid codepoints (numbers). which means they can be written via their codepoints too here is an example:
Codepoints and UTF-8
These numbers are called codepoints for UTF-8 encoding. These Encodings are everywhere. This valid list of codepoints is called a charlist, A charlist is a list of integers where all the integers are valid code points.
And if you are wondering how your device knows how to decode UTF-8 here is how:
- Operating Systems: Systems like Windows, macOS, Linux, and others come with built-in support for UTF-8. This means they can handle text encoded in UTF-8 out of the box.
- Browsers: Web browsers like Microsoft Edge, Firefox, Chrome, and others are designed to understand and render UTF-8 encoded text. They automatically detect the encoding of a webpage based on the HTTP headers or meta tags and display the content accordingly.
- Applications: Software applications, whether they are text editors, email clients, or any other type of program, include UTF-8 support as part of their functionality. Developers implement this standard when creating software to ensure compatibility with text in different languages.
Bitstring
The bitstring is how we store the codepoints (encodings) in our storage (Memory, HHD, SSD) after all computers only understand 0
and 1
. Bitstrings are represented via <<>>\1
syntax. for example here is how we write 4
as bitstring <<4::3>>
it is eq to 100
so this whole syntax thing is for how we represent a binary value.
Some graphemes need more then two bytes to store the character like: “ü” it needs two bytes to be stored. here are the bytes.
The reason when we add <<0>>
to a string we a get a bitstring is that it will no longer be a valid charlist. here is an example with adding 65
codepoint for A
:
Also you can write a valid charlist as a bitstring and get a string: