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

时间:2019-02-23 17:25:12

标签: string dart flutter byte encode

如何在飞镖中读取String的字节? 在Java中,可以通过String方法getBytes()来实现。

See example

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分: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]

runes返回Unicode代码点

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]

答案 2 :(得分:0)

对于图像,它们可能是base64编码的,请使用

input[type=text]:not(.browser-default):focus:not([readonly]) {
    border-bottom: 2px solid var(--yourcolor);
    box-shadow: 0 0 0 0 var(--yourcolor);
}

导入功能

Image.memory(base64.decode('base64EncodedImageString')),