任意の番号システムから任意の長い番号への転送

バイナリ時計 最近、暗号の問題を解決しました。非常に大きな数をある数字体系から別の数字体系に変換する必要がありました。 標準OS計算機は、バイナリ、8進数、10進数、16進数のシステムも処理します。 しかし、それは長い数字のために設計されていません。 そして、 1000文字以上の数字を扱う必要があります。
これらの目的のために、2〜36の任意の長さおよび任意の数値システムで作業できる小さなコンソールコンバーターを作成することにしました。
要件:

•コンバーターは、任意の長さの数値で動作する必要があります。
•コンバーターは、2〜36の任意の数値システムで動作するはずです。
•コンバーターはファイルを操作できる必要があります。

実装:

C ++で書くことにしました。 私はこの言語が大好きで、ソースをC ++から別の言語に翻訳することは難しくありません。
次のクラスを書きました:

class Converter{ private: //    vector<int> a; //   int iriginal; public: //,  2 :   ,    Converter(string str, int original){ this->iriginal = original; //      for ( int i=0; i < str.length(); i++ ){ this->a.push_back(charToInt(str[i])); } } //   ,     -1 int charToInt(char c){ if ( c >= '0' && c <= '9' && (c - '0') < this->iriginal ){ return c - '0'; }else{ if ( c >= 'A' && c <= 'Z' && (c - 'A') < this->iriginal ){ return c - 'A' + 10; }else { return -1; } } } //    char intToChar(int c){ if ( c >= 0 && c <= 9 ){ return c + '0'; }else{ return c + 'A' - 10; } } //        int nextNumber(int final){ int temp = 0; for ( int i = 0; i<this->a.size(); i++){ temp = temp*this->iriginal + this->a[i]; a[i] = temp / final; temp = temp % final; } return temp; } // true -        false    bool zero(){ for ( int i=0; i<this->a.size(); i++ ){ if ( a[i] != 0 ){ return false; } } return true; } //       string convertTo(int final){ vector<int> b; int size = 0; do { b.push_back(this->nextNumber(final)); size++; }while( !this->zero() ); string sTemp=""; for (int i=b.size()-1; i>=0; i--){ sTemp += intToChar(b[i]); } return sTemp; } }; 

コードは非常に簡単です
それから私はそれをプロジェクトにねじ込みました:

 // ,    string inputFile = argv[1]; //   int original = atol(argv[2]); //   int final = atol(argv[3]); //,    string origNumber; ifstream fin(inputFile.c_str()); if ( fin.is_open() ){ fin >> origNumber; }else{ cout << "File " << inputFile << " not open" << endl; //  -   ,       origNumber = inputFile; } fin.close(); Converter conv(origNumber,original); //      ,      if ( argc > 4 ){ //      string outputFile = argv[4]; ofstream fout(outputFile.c_str()); if ( fout.is_open() ){ fout << conv.convertTo(final); }else{ cout << "File " << outputFile << " not create" << endl; cout << conv.convertTo(final) << endl; } }else{ cout << conv.convertTo(final) << endl; } 

もちろん、コードは理想からはほど遠いですが、すべてがシンプルで明確です。
これでテストできます。

コンバータの準備ができましたが、今はテストするために残っています。 1000を表す数値を含むファイルを作成します。
コンソールで起動します。
コンソールでコンバーターを実行する
コンバーターは、長さ3322文字の数値を含むoutput.txtファイルを正常に作成します。
これを画面に表示します。このため、出力用のファイルを指定しなくても十分です。
画面に結果を表示する
コンソールでソース番号を直接設定することもできます
画面2に結果を表示する

結論:

非常に大きなものを変換する必要がある場合、このコンバーターはこれに最適です。 インターフェイスがないため、変更することなく、任意のプラットフォームで記述されたコードを実行できます。 そして、あなたはコンソールであることを忘れることはできません。
プロジェクト(VS 2008)をここからダウンロードします


Source: https://habr.com/ru/post/J146167/


All Articles