-4

Why do I get negative values when I'm trying to convert chars from the string read from file to int?

fin.getline(text, 512);
fin.close();
someInt = (int)text[0]; // It happens to be the random value and always negative.

WHOLE CODE:

The problem itself is in the Decrypt function. When I read the string from file, and convert chars from that string to int, i get negative values, always random, even if characters are the same.

// WUEncryptor.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void Encrypt(string filePath, string password = "");
void Decrypt(string filePath, string password = "");

int main(int argc, char** argv)
{
    int mode = 0;
    string filePath;

    if (argc > 2) {
        mode = atoi(argv[1]);
        filePath = argv[2];

        /** Checking which mode to use */
        switch (mode) {
        case 0:
            if (argc > 3)
                Encrypt(filePath, argv[3]);
            else
                Encrypt(filePath);
            break;

        case 1:
            if (argc > 3)
                Decrypt(filePath, argv[2]);
            else
                Decrypt(filePath);
            break;
        default:
            return 0;
        }
    }
    else {
        /** If something was entered wrong by user, print the error message and quit */
        cout << "Invalid launch..\nYou should use arguments\nFor example: \"WUEncryptor 0 EncryptedFile\n";
    }
    return 0;
}

    /** Encrypting function */
    void Encrypt(string filePath, string password) {
        string text;
        string pass = password;

        /** Quit if pass word isn't determined */
        if (pass == "") {
            cout << "Use \"WUEncryptor 0/1 \"filepath\" password\n";
            return;
        }

        cout << "Enter the message which is going to be encrypted\n";
        char tempstr[512];
        gets_s(tempstr);
        text = tempstr;
        /** Loop that replaces characters in the text */
        for (int i = 0, m = 0, temp = 0; i < text.length(); i++) {

            temp = (int)text[i] + (int)pass[m];
            cout << temp;
            if (temp > 255)
                temp -= 256;
            cout << temp;
            text[i] = (char)temp;

            /** Looping the pass code */
            if (m >= pass.length() - 1)
                m = 0;
            else
                m++;
            cout << "Encrypting " << i << " of " << text.length() << "...\n";
        }
        cout << "Successfully encrypted\nTEXT: " << text << '\n';

        ofstream fout;
        try {
            filePath += ".encrypted";
            cout << "Creating the file...\n";
            fout.open(filePath, ios_base::trunc);
            cout << "Writing to file...\n";
            fout << text;
            fout.close();
            cout << "We're all set!\n";
            system("pause");
        }
        catch (exception) {
            cout << "Something went wrong";
        }
    }

    /** Decrypting function */
    void Decrypt(string filePath, string password) {
        string text, pass;
        pass = password;
        ifstream fin;

        /** Quit if pass word isn't determined */
        if (pass == "") {
            cout << "Use \"WUEncryptor 0/1 \"filepath\" password\n";
            return;
        }
        string openPath = filePath + ".encrypted";

        char tempstr[512];
        try {
            fin.open(openPath);
            fin.getline(&tempstr[0], 512);
            fin.close();
            text = tempstr;
            //cout << (int)text[1];

            for (int i = 0, m = 0, temp = 0; i < text.length(); i++) {

                temp = (int)text[i] - (int)pass[m];
                cout << temp;
                if (temp < 0) 
                    temp += 256;
                cout << temp;
                text[i] = (char)temp;

                /** Looping the pass code */
                if (m >= pass.length() - 1)
                    m = 0;
                else
                    m++;
                cout << "Decrypting " << i << " of " << text.length() << "...\n";
            }
            cout << "Successfully decrypted\nTEXT: " << text << '\n';

        }
        catch (exception) {}

        ofstream fout;
        try {
            string savePath = filePath + ".txt";
            cout << "Creating the file...\n";
            fout.open(savePath, ios_base::trunc);
            cout << "Writing to file...\n";
            fout << text;
            fout.close();
            cout << "We're all set!\n";
            system("pause");
        }
        catch (exception) {
            cout << "Something went wrong";
        }

    }

Someone, clear my mind :D

BeLuckyDaf
  • 11
  • 4

1 Answers1

-2

You're probably using char* as datatype for text. Note that char is signed, so if you (think you) have a value of e.g. 200 there, it's really -72, and so if you cast that to int, you get -72 :)

Marcin Zukowski
  • 4,281
  • 1
  • 19
  • 28
  • I use `string` for `text`. – BeLuckyDaf Jul 12 '16 at 05:32
  • @BeLuckyDaf `std::string operator[]` operator returns a `char`, and it's implementation-defined whether this is signed or unsigned. It's apparently signed on your system. – Barmar Jul 12 '16 at 05:48
  • 3
    @BeLuckyDaf -- don't go down that rabbit hole. This answer is just a guess. Until you provide real code that compiles, runs, and shows the problem, nobody can tell you what's going on. – Pete Becker Jul 12 '16 at 06:08
  • @PeteBecker check it out now, please :) – BeLuckyDaf Jul 12 '16 at 07:08