如何在 flutter 中将二进制字符串转换为文本字符串以及相反?

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

我想要做的是输入像

["01001000 01100101 01111001"]
这样的字符串并将其转换为
["Hey"]
或相反,输入
["Hey"]
并将其转换为
["01001000 01100101 01111001"]

flutter dart binary type-conversion
2个回答
7
投票
String encode(String value) {
  // Map each code unit from the given value to a base-2 representation of this
  // code unit, adding zeroes to the left until the string has length 8, and join
  // each code unit representation to a single string using spaces
  return value.codeUnits.map((v) => v.toRadixString(2).padLeft(8, '0')).join(" ");
}

String decode(String value) {
  // Split the given value on spaces, parse each base-2 representation string to
  // an integer and return a new string from the corresponding code units
  return String.fromCharCodes(value.split(" ").map((v) => int.parse(v, radix: 2)));
}

void main() {
  print(encode("Hey"));    // Output: 01001000 01100101 01111001
  print(decode("01001000 01100101 01111001"));    // Output: Hey
}


0
投票

有没有二进制转文本或二进制转pdf的方法?我已经搜索了一段时间但找不到如何做到这一点。

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