Why is this string invalid?

iex> String.valid?(<<239, 191, 19>>)

Can u please explain why this string is invalid?

1 Like

It’s not a valid utf8 sequence. 239 starts with bits 1110 which requires 2 continuation bytes. The second byte (19) is not a valid continuation byte since it does not start with the bit pattern 10 (it is 0b00010011).


I’ve added codefences such that one can see full code now.

Also the answer to your question is simple:

It is not a valid UTF-8 encoded sequence.

The first byte starts with 1110, so we need three bytes to decode this character, we have three bytes here. The second and third byte have to start with 10 each, so they have to be >= 128 && <192.

Your last byte obviously doesnt fit.

Source: https://en.wikipedia.org/wiki/UTF-8

1 Like