Skip to main content

L1330 - Truncated Character in Binary String

Warning​

karate() ->
<<"η©Ίζ‰‹πŸ™‚">>.
%% ^^^^^^^^^ warning: character code larger than 255 will be truncated

Explanation​

A binary text segment with no /utf8, /utf16, or /utf32 specifier is encoded using one byte per character (Latin-1), for historical reasons. Characters with code points above 255 do not fit in a single byte and are truncated, so the resulting binary represents different characters than the ones in the source code.

In the example, <<"η©Ίζ‰‹πŸ™‚">> is silently encoded as <<"zKB">>.

%% Instead of:
karate() -> <<"η©Ίζ‰‹πŸ™‚">>.

%% Write (UTF-8):
karate() -> <<"η©Ίζ‰‹πŸ™‚"/utf8>>.

%% Or, since OTP 27, simply:
karate() -> ~"η©Ίζ‰‹πŸ™‚".

This warning can be suppressed with -compile(nowarn_truncated_character), but note that the truncated result is almost never what you want. See also L1329.