From 2bc7e68e20c8d5941097bbdcd535dc2403a4219b Mon Sep 17 00:00:00 2001 From: Saleem Ahmad Date: Sat, 19 May 2018 12:56:10 +0530 Subject: [PATCH] Add convert_to_base_64 function to convert string integer to magnitudes --- include/BigInt.hpp | 1 + include/constructors/constructors.hpp | 15 +++------------ include/functions/conversion.hpp | 16 ++++++++++++++++ include/functions/utility.hpp | 1 + 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/include/BigInt.hpp b/include/BigInt.hpp index 0ae1602..bfedfc5 100644 --- a/include/BigInt.hpp +++ b/include/BigInt.hpp @@ -100,6 +100,7 @@ class BigInt { int to_int() const; long to_long() const; long long to_long_long() const; + std::std::vector to_base64(); // Random number generating functions: friend BigInt big_random(size_t); diff --git a/include/constructors/constructors.hpp b/include/constructors/constructors.hpp index 4d64c60..550700e 100644 --- a/include/constructors/constructors.hpp +++ b/include/constructors/constructors.hpp @@ -53,12 +53,7 @@ BigInt::BigInt(const std::string& num) { if (num[0] == '+' or num[0] == '-') { // check for sign std::string num_magnitude = num.substr(1); if (is_valid_number(num_magnitude)) { - /* - TODO - ---- - magnitude = convert_to_base_2_to_the_64(num_magnitude); - */ - magnitude = {0}; + magnitude = convert_to_base_64(num_magnitude); is_negative = num[0] == '-'; } else { @@ -67,12 +62,8 @@ BigInt::BigInt(const std::string& num) { } else { // if no sign is specified if (is_valid_number(num)) { - /* - TODO - ---- - magnitude = convert_to_base_2_to_the_64(num_magnitude); - */ - magnitude = {0}; + std::string num_magnitude(num); + magnitude = convert_to_base_64(num_magnitude); is_negative = false; // positive by default } else { diff --git a/include/functions/conversion.hpp b/include/functions/conversion.hpp index 37387c7..b245ccf 100644 --- a/include/functions/conversion.hpp +++ b/include/functions/conversion.hpp @@ -58,4 +58,20 @@ long long BigInt::to_long_long() const { return std::stoll(this->to_string()); } +/* + to_base64 + --------- + Convers a BigInt to its base64 representation +*/ + +std::vector BigInt::to_base64() { + std::vector magnitude; + BigInt max_number("18446744073709551616"); // 2^64 for base conversion + while (*this != 0) { + magnitude.push_back(*this % max_number); + *this /= max_number; + } + return magnitude; +}; + #endif // BIG_INT_CONVERSION_FUNCTIONS_HPP diff --git a/include/functions/utility.hpp b/include/functions/utility.hpp index a26ed45..60d4768 100644 --- a/include/functions/utility.hpp +++ b/include/functions/utility.hpp @@ -8,6 +8,7 @@ #define BIG_INT_UTILITY_FUNCTIONS_HPP #include +#include /*