如何在飞镖中读取String
的字节?在Java中,有可能通过String
方法getBytes()
。
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);
有一个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]