Encoding.UTF8.GetString returns a string with Length = -1

I don’t think this should be possible, and it is different from desktop .NET. In my case, it happens when the byte[ ] array contains some 0xFF bytes.

I think this post was seeing similar problems.

0xFF is a special case with UTF-8. Google for more info.

In the earlier thread I linked to, another user reported similar problems with the byte array { 0x41, 0xe9, 0x42,0x43,0x44, 0x0d, 0x0a }. I have tested that and confirmed it myself.

As for 0xFF, it still should count as a character, and a string should never have a length of -1 in C#. Try it in desktop .NET and you will see.

I believe you.

1 Like