2017-04-13 3 views
-3

私は入力された文字列を取り込み、文字を整数に変換する必要があります(a = 1、b = 2、c = 3等)を求めて出力する。個々の文字を基本的なパスワードハッシャーの整数に変換する方法C++

これまで私が持っていたのは、整数に変換する文字列を使用する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. 
    } 
} 

乾杯、

オーウェン。

+0

int char_value = tempPassword [i] - 'a' + 1 – Meccano

+0

文字には[数値](http://www.asciitable.com/)が含まれています。たとえば、 'tempPassword [i] - 'a''は' tempPassword'の各文字の 'a'からのオフセットを与えます。 –

+2

_But私はすでに、いくつかのエラーを越えて、これを構築しようとしています。あなたはエラーメッセージを含めませんでした。なぜですか? –

答えて

0

C++では、文字を数値として操作できます。ここで

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

は、私は例として、「X」を使用:だからたとえば、次のような何かを行うことができます。小文字の場合、nは0〜25の数値になります。 1から26を代わりに使用する場合は、1を追加してください。

-1

このコードをコンパイルできないのは、メイン関数が定義されていないからです。それを追加すると、 "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 
} 

私は、Linuxを使用していたので、私はWINDOWS.Hを削除しました。 Windowsでコンパイルするときは、自由に追加してください。

関連する問題