我想做的是输入一个字符串,如["01001000 01100101 01111001"],并将其转换为["Hey"]或相反,输入["Hey"],并将其转换为["01001000 01100101 01111001"]
["01001000 01100101 01111001"]
["Hey"]
jm2pwxwz1#
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}
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
1条答案
按热度按时间jm2pwxwz1#