This module contains functions for converting between different character representations. Basically it converts between iso-latin-1 characters and Unicode ditto, but it can also convert between different Unicode encodings (like UTF-8, UTF-16 and UTF-32).
The default Unicode encoding in Erlang is in binaries UTF-8, which is also the format in which built in functions and libraries in OTP expect to find binary Unicode data. In lists, Unicode data is encoded as integers, each integer representing one character and encoded simply as the Unicode codepoint for the character.
Other Unicode encodings than integers representing codepoints or UTF-8 in binaries are referred to as "external encodings". The iso-latin-1 encoding is in binaries and lists referred to as latin1-encoding.
It is recommended to only use external encodings for communication with external entities where this is required. When working inside the Erlang/OTP environment, it is recommended to keep binaries in UTF-8 when representing Unicode characters. Latin1 encoding is supported both for backward compatibility and for communication with external entities not supporting Unicode character sets.
unicode_binary() = binary() with characters encoded in UTF-8 coding standard
unicode_char() = integer() representing valid unicode codepoint
chardata() = charlist() | unicode_binary()
charlist() = [unicode_char() | unicode_binary() | charlist()]
a unicode_binary is allowed as the tail of the list
external_unicode_binary() = binary()
with characters coded in a user specified Unicode encoding other
than UTF-8 (UTF-16 or UTF-32)
external_chardata() = external_charlist() | external_unicode_binary()
external_charlist() = [unicode_char() | external_unicode_binary() | external_charlist()]
an external_unicode_binary is allowed as the tail of the list
latin1_binary() = binary() with characters coded in iso-latin-1
latin1_char() = integer() representing valid latin1 character (0-255)
latin1_chardata() = latin1_charlist() | latin1_binary()
latin1_charlist() = [latin1_char() | latin1_binary() | latin1_charlist()]
a latin1_binary is allowed as the tail of the list
Check for a UTF byte order mark (BOM) in the beginning of a
binary. If the supplied binary
If no BOM is found, the function returns
Same as characters_to_list(Data,unicode).
This function converts a possibly deep list of integers and
binaries into a list of integers representing unicode
characters. The binaries in the input may have characters
encoded as latin1 (0 - 255, one character per byte), in which
case the
If
The purpose of the function is mainly to be able to convert
combinations of unicode characters into a pure unicode
string in list representation for further processing. For
writing the data to an external entity, the reverse function
The option
If for some reason, the data cannot be converted, either
because of illegal unicode/latin1 characters in the list, or
because of invalid UTF encoding in any binaries, an error
tuple is returned. The error tuple contains the tag
However, if the input
Errors occur for the following reasons:
A special type of error is when no actual invalid integers or
bytes are found, but a trailing
If one UTF characters is split over two consecutive
binaries in the
decode_data(Data) ->
case unicode:characters_to_list(Data,unicode) of
{incomplete,Encoded, Rest} ->
More = get_some_more_data(),
Encoded ++ decode_data([Rest, More]);
{error,Encoded,Rest} ->
handle_error(Encoded,Rest);
List ->
List
end.
Bit-strings that are not whole bytes are however not allowed, so a UTF character has to be split along 8-bit boundaries to ever be decoded.
If any parameters are of the wrong type, the list structure
is invalid (a number as tail) or the binaries does not contain
whole bytes (bit-strings), a
Same as characters_to_binary(Data, unicode, unicode).
Same as characters_to_binary(Data, InEncoding, unicode).
This function behaves as
The option
Errors and exceptions occur as in
Create an UTF byte order mark (BOM) as a binary from the
supplied
The function returns
It can be noted that the BOM for UTF-8 is seldom used, and it is really not a byte order mark. There are obviously no byte order issues with UTF-8, so the BOM is only there to differentiate UTF-8 encoding from other UTF formats.