Invalid UTF-8 byte (at offset 6)

huangapple go评论77阅读模式
英文:

FormatException: Invalid UTF-8 byte (at offset 6)

问题

为什么我在这一行代码中出现错误?

final pdfString = "" (T) 20.5 (–H) 4 (E) ]     % 5-element array of strings, reals, and intege"";
print(utf8.decode(pdfString.codeUnits)); 

为什么我会得到以下错误信息?

FormatException: Invalid UTF-8 byte (at offset 11)

我是从 O'Reilly 的书中复制这个字符串的。

无论上下文如何,如果我只有它的字节表示,如何恢复 pdfString

英文:

Why do I get an error with this single line of code?

    final pdfString = " (T) 20.5 (–H) 4 (E) ]     % 5-element array of strings, reals, and intege";
    print(utf8.decode(pdfString.codeUnits)); 

why I get

> FormatException: Invalid UTF-8 byte (at offset 11)

I copied this string from O'Reilly book.

And independently of the context, how to recover the pdfString if I have its byte representation only ?

答案1

得分: 0

根据String.codeUnits的文档:

一个字符串的UTF-16代码单元的不可修改列表。

(已强调。)

实际上,您正在对一个UTF-16字符串调用utf8.decode(),这是行不通的。

如果您提供关于您实际尝试做什么的更多上下文,我可能可以提供建议。

英文:

As per the documentation for String.codeUnits:

> An unmodifiable list of the UTF-16 code units of this string.

(Emphasis added.)

In effect, you are calling utf8.decode() on a UTF-16 string, which is not going to work.

If you provide more context about what you're actually trying to do, I may be able to offer a suggestion.

huangapple
  • 本文由 发表于 2023年3月7日 09:30:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定