Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convert_to_base_64 function to convert string integer to magnitudes #34

Open
wants to merge 1 commit into
base: base-2-to-the-64
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/BigInt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class BigInt {
int to_int() const;
long to_long() const;
long long to_long_long() const;
std::std::vector<uint64_t> to_base64();

// Random number generating functions:
friend BigInt big_random(size_t);
Expand Down
15 changes: 3 additions & 12 deletions include/constructors/constructors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we have a circular dependency here, because in order to get magnitudes we first need to create a BigInt from string and then get magnitudes by calling a function which returns a magnitude vector, in my case BigInt::to_base64. What are your thoughts on this?

is_negative = num[0] == '-';
}
else {
Expand All @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions include/functions/conversion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t> BigInt::to_base64() {
std::vector<uint64_t> 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
1 change: 1 addition & 0 deletions include/functions/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define BIG_INT_UTILITY_FUNCTIONS_HPP

#include <tuple>
#include <BigInt.hpp>


/*
Expand Down