=0; i--){ outpu-6ren">
gpt4 book ai didi

c++ - 不混淆的字符串函数

转载 作者:行者123 更新时间:2023-11-28 06:45:26 27 4
gpt4 key购买 nike

假设我有一串字符:"

String input = "5;5;5;5;"

并将它们插入到这个函数中:

for(int i=input.length()-1; i>=0; i--){
output += (QChar)((input[i].unicode() + 32) + 8160);
}

return output;

输出:※‵※‵※‵※‵

我将如何编写解密函数?

我知道我可以做到:

for(int i=input.length()-1; i>=0; i--){
output += (QChar)(input.unicode()-32 - 8160);
}
return output;

但为什么这样行得通:

for(int i=input.length()-1; i>=0; i--){
output += (QChar)((input.unicode()-32)%255); //Get ASCII character
}
return output;

这只是巧合吗?或者我只是不了解数学。我知道尝试反转模运算几乎是不可能的,除非您知道您想要的 x 的范围。

(1 + x) % 4 = 2

x 可以是 5,9,13,...

最佳答案

第二个解密函数起作用是因为8160正好是255的倍数,原始值包含在0-254范围内。

查看您的示例 x 对于任何 n 可以是 1+4n 形式的任何数字。类似地,对于第二个解密函数,input.unicode()-32+255*n % 255 对于 n 的任何小值(即这么长因为在计算期间没有溢出),包括 n=-32 对应于 255*(-32)=-8160 的情况。

关于c++ - 不混淆的字符串函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25099609/

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