简体   繁体   English

我正在使用模数来处理密码,字母“ u”无法正确解码

[英]I'm working on a cipher code using modulos and the letter “u” won't decode properly

this is the code when I run it "u" won't show if I type in "under" it will decode as "nder" I'm not sure whether it's the use of modulus or the math in my problem I've tried with many other letters and the only real issue is with "u" 这是我运行它时的代码,如果我键入“ under”,则不会显示,它将解码为“ nder”。我不确定是我尝试过的问题中使用的是模数还是数学还有很多其他字母,唯一真正的问题是“ u”

#include <iostream>

std::string decode(std::string input, int shift_amount);

std::string encode(std::string input, int shift_amount);

int main(int argc, char **argv)
{
    std::string x = "{qr-or-{rny";
    std::string output;
    output = decode(x, 13);
    std::cout << output << std::endl;
}

std::string encode(std::string input, int shift_amount)
{
    for (int i = 0; i < input.length(); i++)
    {
       input[i] = (input[i] + shift_amount) % 255;
    }
    return input;
}

std::string decode(std::string input, int shift_amount)
{
    for (int i = 0; i < input.length(); i++)
    {
        input[i] = (input[i] - shift_amount) % -255;
    }
    return input;
}

Simple test: 简单测试:

#include <iostream>

int
main()
{
  std::cout << (- 13) % (- 255);;
  return 0;
}

The program prints -13. 程序打印-13。

The % operation is not the modulo operation , but the remainder: you will not go back to a positive number from a negative one. %运算不是取模运算 ,而是余数:您不会从负数返回正数。 You have to add 255 for negative numbers. 您必须为负数添加255。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不确定为什么这段代码不起作用? - I'm not sure why this code isn't working? 我的C ++代码中的Letter计数器无法正常工作 - The Letter counter within my C++ code is not working properly 我正在尝试通过打印它来检查类向量的getter是否正常工作但是它不起作用 - I'm trying to check if a getter for a class vector is working by printing it but it won't work C++“相同字母”代码无法正常工作 - C++ “Same Letter” code doesn't work properly 我试图总结给定数组中的数字,使用递归,尝试了这段代码,但它不起作用。 为什么? - I'm trying to sum up the numbers in a given array, using recursion , tried this code but it isn't working. Why? C++ 需要关于(为什么它不能正常工作)我的基本文本控制台游戏(我是初学者)的建议。 不像我想要的那样工作 - C++ Need advice on (why it isn't working properly) my basic text console game (I'm an beginner). Not working as I want it to (Q)OpenGL代码无法正确打开窗口 - (Q) OpenGL Code won't open window properly 我正在尝试编写一个 class,其中子 class 将继承父 class 的方法,但我的代码不会编译 - I'm trying to write a class where the child class will inherit the methods from the parent class, but my code won't compile 我正在尝试学习如何正确分离类头和代码 - I'm trying to learn how to properly separate class header and code 我在循环中的循环中遇到问题无法正常工作 - I'm having a problem in a loop in a loop not working properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM