如何将单个字符转换为为基本密码哈希器设置整数,C++

How to convert individual chars to set integers for a basic password hasher, C++

本文关键字:哈希器 密码 设置 整数 C++ 单个 字符 转换      更新时间:2023-10-16

我正在为一些 uni 工作制作密码哈希,这需要我获取一个输入的字符串,将其中的字符转换为整数(给定 a=1、b=2、c=3 等(并输出总和。

这就是我到目前为止所拥有的:一个将返回一个 int 的函数,它使用一个字符串从中转换为整数。但是我已经在尝试构建它时遇到了一些错误,我不知道如何从这里开始转换角色。

#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>
using namespace std;
int passHasher(string tempPassword)
{
    int hashValue = 0; //function will return this at the end of the passes.
    for (int i = 0; i < tempPassword.size; i++) 
    {
        //hashing algorythm goes here.
    }
}

干杯

欧文。

在C++中,字符可以作为其数值进行操作。例如,您可以执行以下操作:

char c = 'x';
int n = c - 'a'.

在这里,我以"x"为例。对于任何小写字母,n将是介于 0 和 25 之间的数字(包括 0 和 25(。如果您想要 1 到 26,只需添加 1。

无法编译此代码的原因是没有定义 main 函数。添加后,您将在使用"size"方法时出现错误。大小是一种方法,因此最后需要 ((。

以下代码编译良好:

#include <iostream>
#include <string>
using namespace std;
int passHasher(string tempPassword)
{
    int hashValue = 0; //function will return this at the end of the passes.
    for (int i = 0; i < tempPassword.size(); i++) 
    {
        //hashing algorythm goes here.
    }
    return 0;
}
int main() {
    // get the string fromt the input and call function
}

我删除了windows.h,因为我使用的是Linux。在Windows上编译时,请随意添加它。