Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

c++ - Why does this loop in my decryption function not work?

So the problem I'm facing is that a loop which is supposed to be converting a bunch of numbers(f.e. 12345) to an integer, so that the integer would have the value of 12345 in this case, but it just behaves weirdly...sometimes it works, sometimes it doesn't or sometimes it just fails in the middle...f.e.: (numbers separated by a period: encrypted string)

Expected Output:

377733.204118
3
37
377
3777
37773
377733
.
2
20
204
2041
20411
204118

Actual Output:

377733.204118
3
7
7
7
3
3
.
2
20
4
41
411
8

And here's the function(I don't know how to make a more simple example for the problem, so it's just the whole function...):

static std::string decrypt(const char* text, unsigned long long n, unsigned long long d) {
        std::string dtext;
        char cchar;
        unsigned long long cint;
        for (unsigned long long i = 0;;) {
            if (i == strlen(text)) {
                break;
            }
            cint = 0;
            for (unsigned long long j = i; j < strlen(text);j++) {
                if (text[j] == (char)'.') {
                    i++;
                    std::cout << "." << std::endl;
                    break;
                }
                cint = cint * 10;
                switch (text[j]) {
                case 48:
                    break;
                case 49:
                    cint++;
                    break;
                case 50:
                    cint = +2;
                    break;
                case 51:
                    cint = +3;
                    break;
                case 52:
                    cint = +4;
                    break;
                case 53:
                    cint = +5;
                    break;
                case 54:
                    cint = +6;
                    break;
                case 55:
                    cint = +7;
                    break;
                case 56:
                    cint = +8;
                    break;
                case 57:
                    cint = +9;
                    break;
                }
                i++;
                std::cout << '' << cint << std::endl;
            }
            for (unsigned long long j = d - 1; j > 0; j--) {
                cint = cint * cint;
                cint = cint % n;
            }
            cchar = cint;
            dtext.push_back(cchar);
        }
        return dtext;
    }

And a folder with code to test(folder with main file and header with function(the function is at the bottom of the header)): https://www.mediafire.com/file/k5m3pozt1hfv7eb/Example.zip/file

PS: I've already tested std::stoi, but it gave me the ASCII values of the numbers(48 for 0, 49 for 1, ...) when I tried it in the decrypt-function even though it worked when I tried it in the main function...(replace the switch(text[j]) {...} with cint = std::stoi(std::to_string(text[j])); if you want to try it)

question from:https://stackoverflow.com/questions/66067994/why-does-this-loop-in-my-decryption-function-not-work

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, strictly speaking your problem is that you're using cint = +x within your switch statement, which sets cint to x, whereas you want to use cint += x which would add x to cint. You should throw out the whole switch statement altogether though, and replace it with cint += text[j] - '0' which does the same thing.

You can do regular integer arithmetic with characters, no need for extensive switch statements.

EDIT: Oh and as 1201ProgramAlarm pointed out you should make sure that text[j] is a digit. This is what you could replace your switch statement with:

if (text[j] >= '0' && text[j] <= '9') {
     cint += text[j] - '0';
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...