flutter - 如何将二进制字符串转换为文本字符串并在 flutter 中反转?

我想做的是输入一个像["01001000 01100101 01111001"]这样的字符串并将它转换成["Hey"]或者相反的,输入["Hey"] 并将其转换为 ["01001000 01100101 01111001"]

最佳答案

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
}

https://stackoverflow.com/questions/68038979/

相关文章:

flutter - Flutter 中 colorScheme 和 ThemeData 声明的原色

react-native - SectionList React Native 上的粘性 heade

java - 设置标题背景 Vaadin 14 网格

javascript - Next JS 和 Cloudinary,图像未加载

reactjs - 难以创建基本线(@react-three/fiber 和 Typescript)

flutter - flutter中的flutter build bundle和appbundle有

delphi - Spring4D TDistinctIterator.ToArra

kotlin - 如何在 Kotlin Exposed 中获取插入的行?

c++ - 为什么默认参数在模板函数中不起作用?

c++ - 无法将 'v8::MaybeLocal' 转换为 'v8::Lo