📜 ⬆️ ⬇️

Translation from any number system to any numbers of great length

Binary Clock Recently I solved problems in cryptography, and it became necessary to transfer very large numbers from one number system to another. With the binary, octal, decimal and hexadecimal system and handles the standard calculator OS. But it is not designed for numbers of great length. And I just need to work with numbers of length > 1000 characters.
For these purposes, I decided to write a small console converter that allows you to work with numbers of any length and any number system from 2 to 36.
Requirements:

• The converter should work with numbers of any length.
• The converter should work in any number system from 2 to 36.
• The converter should be able to work with files.

Implementation:

I decided to write in C ++. I love this language, and it is easy to translate the source code into another language from C ++.
Wrote the following class:

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; } }; 

The code is pretty simple.
Next, screw it into the project:

 // ,    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; } 

The code, of course, is far from ideal, but everything is simple and clear.
Now you can test.

The converter is ready, now it is necessary to test it. I create a file containing a number, which is a thousand nines.
I run in the console:
Running the converter in the console
The converter successfully creates the file output.txt containing a number 3322 characters long.
Now I will display it on the screen, for this it is enough not to specify the file for output.
Displaying the result on the screen
You can also set the initial number directly in the console
Displaying the result on screen 2

Conclusion:

If you need to convert something very large, then this converter is perfect for this. The absence of the interface allows you to run written code on any platform without change. A console can not be forgotten, whether windows or Linux ...
Download the project (VS 2008) here .

')

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


All Articles