lrn*_*lrn 10
你需要说出你需要的格式.字符串是16位整数的序列.在某些设置中,它们被解释为UTF-16,其中一些整数(代码单元)代表单个代码点,其他代码点需要两个代码单元.在其他设置中,"字符"不止一个代码点.因此,要将字符串转换为位,您需要准确了解如何解释字符串.
这里只为"a"写入8位,而不是"000000000110001",因此我假设您只考虑ASCII字符串.
我们假设它是全ASCII,你想要八位结果:
var binstring = string.codeUnits.map((x) => x.toRadixString(2).padLeft(8, '0'))
.join();
Run Code Online (Sandbox Code Playgroud)
听起来您正在寻找 javascriptString.charCodeAt
方法的 Dart 等效项。在 Dart 中有两个选项,具体取决于您是想要整个字符串的字符代码还是字符串中的单个字符。
String.codeUnitAt
是一种方法,它为字符串中给定索引处的字符返回一个 int。您可以按如下方式使用它:
String str = 'hello';
print(str.codeUnitAt(3)); //108
Run Code Online (Sandbox Code Playgroud)
您可以使用String.codeUnits
将整个字符串转换为其整数值:
String str = 'hello';
print(str.codeUnits); //[104, 101, 108, 108, 111]
Run Code Online (Sandbox Code Playgroud)
如果您需要将这些整数转换为其相应的二进制字符串,您需要int.toRadixString
使用 2 的参数
String str = 'hello';
List<String> binarys = str.codeUnits.map((int strInt) => strInt.toRadixString(2));
print(binarys); //(1101000, 1100101, 1101100, 1101100, 1101111)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1376 次 |
最近记录: |