Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp https://pastebin.com/vV3VfRGF
- // Convert.h https://pastebin.com/39AvSGaj
- #include "Convert.h"
- Convert::Convert(){
- _base = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- }
- // Converts a number from any base (binary, hex, etc.) to a decimal (base 10) number.
- int Convert::tonumber(string input, int base){
- int num = 0;
- for(int i = 0; i < input.length(); i++){
- for(int b = 0; b < base; b++){
- if(input[i] == _base[b]){
- num += b * pow(base, input.length() - i - 1);
- break;
- }
- }
- }
- return num;
- }
- // Converts a decimal (base 10) number to a number of any base (binary, hex, etc.).
- string Convert::tobase(int input, int base){
- string num = "";
- do{
- num = _base[input % base] + num;
- input = floor(input / base);
- }while(input > 0);
- return num;
- }
- // Gets the number of characters available with the given base size.
- string Convert::getBase(int baseSize){
- string base = _base.substr(0, baseSize);
- return base;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement