我想做的是输入一个像["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
javascript - Next JS 和 Cloudinary,图像未加载
reactjs - 难以创建基本线(@react-three/fiber 和 Typescript)
flutter - flutter中的flutter build bundle和appbundle有
delphi - Spring4D TDistinctIterator