如何在Dart中获取字符串的字节?

问题描述 投票:2回答:2

如何在飞镖中读取String的字节?在Java中,有可能通过String方法getBytes()

See example

string dart flutter byte encode
2个回答
5
投票
String foo = 'Hello world';
List<int> bytes = utf8.encode(foo);
print(bytes);

输出:[72,101,108,108,111,32,119,111,114,108,100]

另外,如果你想转换回来:

String bar = utf8.decode(bytes);

1
投票

有一个codeUnits吸气剂返回UTF-16

String foo = 'Hello world';
List<int> bytes = foo.codeUnits;
print(bytes);

[72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]

和返回Unicode代码点的runes

String foo = 'Hello world';
// Runes runes = foo.runes;
// or
Iterable<int> bytes = foo.runes;
print(bytes.toList());

[72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]

© www.soinside.com 2019 - 2024. All rights reserved.