Encodings and Unicode

Contents
UTF-8 binaries
Latin-1 and the old days
The unicode module
Rules of thumb

UTF-8 binaries

Modern Erlang stores text as UTF-8 binaries. A literal string with non-ASCII characters is a binary holding the UTF-8 bytes.

String functions from the string module understand Unicode and count characters, not bytes.

1> "caf" ++ <<233:8>>.

<<"caf"/utf8>>

2> string:length("caf" ++ <<233:8>>).

4

Latin-1 and the old days

Old Erlang printed charlists as Latin-1 by default, which is why legacy code mentions latin1. Binaries and lists still convert through the encoding modules.

New code picks one encoding, usually UTF-8, and converts at the edges only.

1> unicode:characters_to_list("caf" ++ <<233:8>>).

[99,97,102,233]

The unicode module

The unicode module converts between binaries, charlists, and UTF-16 or UTF-32. Its functions answer either the converted data or an error tuple.

Convert once at the boundary, then work in your normal representation inside.

1> unicode:characters_to_binary(["caf", 233]).

<<"caf"/utf8>>

2> unicode:characters_to_nfc_binary("cafe" << 769:8>>).

<<"caf"/utf8>>

Rules of thumb

Keep binaries for data in and out, and only convert to charlists when an old API demands one. Never mix encodings inside one program.

When text looks broken, check the encoding at the boundary before blaming the logic.

1> upper(Bin) ->

string:uppercase(Bin).

Next: Time and date

Article author: Arthur Isaev

Related articles
Erlang
Erlang course: zero to hero in 50 lessons
What is Erlang and where it runs
Install Erlang and check the version
erl: the interactive shell
Your first Erlang script
Variables and single assignment
Numbers
Atoms
Strings and binaries
Truthiness: only false and nil
Conditions: case and if
Recursion replaces loops
Lists
Tuples
Maps
Functions and clauses
Pattern matching
Guards
Records
Exceptions: try and catch
Modules, exports and imports
Funs: anonymous functions
Higher-order functions
Keyword lists
Erlang idioms and clean code
Rebar3 builds
Testing with EUnit
Debugging
Encodings and Unicode
Time and date
JSON and term formats
Processes and message passing
Spawn and links
Monitors and exits
GenServer
Supervisors
OTP applications
Files and IO
Sockets
HTTP clients
io:format and output
Let it crash: error handling
ETS tables
Timers and timeouts
Code loading
Distributed Erlang
Capstone: todo CLI
Capstone: TCP server
Performance
Modern Erlang tour
Hero roadmap: the whole course on one page
Install Erlang on Windows 11
Install Erlang on Ubuntu
Install Erlang on Rocky Linux
Install Erlang on macOS
Install Erlang on FreeBSD
Erlang in Docker
Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: