如何将二进制字符串转换为文本字符串并在 flutter 中反转?
How to convert a binary string to text string and the reverse in 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
}
我想做的是输入一个像["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
}