如何将utf8代码单元编码为String并回读Flutter
How to encode utf8 code units to String and read back Flutter
我的用户输入是一个字符串,它也有表情符号,所以我将它转换为一个 utf8 代码单元列表。
当我将此响应发送到服务器时,它会将其存储为 String
"[60, 115, 116, 114, 111, 110, 103, 62, 114, 105, 99, 104, 32, 116,
101, 120, 116, 240, 159, 152, 137, 240, 159, 152, 137, 240, 159, 152,
137, 240, 159, 152, 137, 240, 159, 152, 137, 60, 47, 115, 116, 114,
111, 110, 103, 62, 60, 98, 114, 62, 60, 98, 114, 62]"
但是当我尝试对此进行解码时,我不能,因为 utf8.decode(CodeUnits)
需要一个列表输入,但我的输入现在是一个字符串。
我正在使用以下方法将此字符串编码为 utf8:
utf8.encode("My String here");
如果有更好的存储和显示表情符号的方法,请告诉我。
我们在 API.
中使用 Django Rest Framework
调用您的字符串 temp,您可以通过以下方式将 temp 转换为列表:
List<int> intList = List<int>.from(json.decode(temp));
然后使用以下方法将其转换为 UTF8:
String result = new Utf8Decoder().convert(intList);
print(result);
结果:
<strong>rich text</strong><br><br>
更密集的答案是:
String result = new Utf8Decoder().convert(List<int>.from(json.decode(temp)))
我的用户输入是一个字符串,它也有表情符号,所以我将它转换为一个 utf8 代码单元列表。 当我将此响应发送到服务器时,它会将其存储为 String
"[60, 115, 116, 114, 111, 110, 103, 62, 114, 105, 99, 104, 32, 116, 101, 120, 116, 240, 159, 152, 137, 240, 159, 152, 137, 240, 159, 152, 137, 240, 159, 152, 137, 240, 159, 152, 137, 60, 47, 115, 116, 114, 111, 110, 103, 62, 60, 98, 114, 62, 60, 98, 114, 62]"
但是当我尝试对此进行解码时,我不能,因为 utf8.decode(CodeUnits)
需要一个列表输入,但我的输入现在是一个字符串。
我正在使用以下方法将此字符串编码为 utf8:
utf8.encode("My String here");
如果有更好的存储和显示表情符号的方法,请告诉我。 我们在 API.
中使用 Django Rest Framework调用您的字符串 temp,您可以通过以下方式将 temp 转换为列表:
List<int> intList = List<int>.from(json.decode(temp));
然后使用以下方法将其转换为 UTF8:
String result = new Utf8Decoder().convert(intList);
print(result);
结果:
<strong>rich text</strong><br><br>
更密集的答案是:
String result = new Utf8Decoder().convert(List<int>.from(json.decode(temp)))