C++:将字符转换为 int 时获取随机负值
C++: Getting random negative values when converting char to int
为什么当我尝试将从文件读取的字符串转换为 int 时,我得到负值?
fin.getline(text, 512);
fin.close();
someInt = (int)text[0]; // It happens to be the random value and always negative.
整个代码:
问题本身出在解密功能上。当我从文件中读取字符串并将字符从该字符串转换为 int 时,我得到负值,总是随机的,即使字符相同。
// 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 argumentsnFor example: "WUEncryptor 0 EncryptedFilen";
}
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" passwordn";
return;
}
cout << "Enter the message which is going to be encryptedn";
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 encryptednTEXT: " << 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" passwordn";
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 decryptednTEXT: " << 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";
}
}
有人,让我清醒:D
您可能正在使用char*
作为text
的数据类型。请注意,char
是有符号的,所以如果你(认为你)的值是例如 200
那里,它真的很-72
,所以如果你把它投给int
,你会得到-72
:)
相关文章:
- 为什么我的 2d 数组的第二行从 RAM 中获取随机值?
- 如何在 C++ 中获取随机函数的周期性
- C 编程:在循环时运行 2 并从 loop1 获取数据的随机结果
- 有什么方法可以从C 中的Enum类获取随机
- 从容器中获取随机元素,该容器在恒定时间内没有严格的元素顺序
- 在新数组中获取随机值
- 从满足谓词的迭代器中均匀随机的容器中获取迭代器
- 根据值的概率从 Map 获取随机键
- 试图从文件中获取随机数字的总和
- 无法获取随机字符串
- 如何从向量中获取随机和唯一值?
- Qhash-热以获取随机键和价值
- 如何从指针列表中获取随机指针
- 获取随机科学数字而不是输入
- 从 C++ 中的 [0,1] 中获取随机双精度数
- 获取随机元素并将其移除
- C++ Int 在不应该更改它的函数后获取随机值
- 这是从容器中获取随机元素的OK范围的方法吗
- 使用Winsock获取随机/奇怪的数据
- C++:将字符转换为 int 时获取随机负值