gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:32:28 26 4
gpt4 key购买 nike

我想做的是输入一个像["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
}

关于flutter - 如何将二进制字符串转换为文本字符串并在 flutter 中反转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68038979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com