Skip to content

Commit 2bc7e68

Browse files
committed
Add convert_to_base_64 function to convert string integer to magnitudes
1 parent 20c8173 commit 2bc7e68

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

include/BigInt.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class BigInt {
100100
int to_int() const;
101101
long to_long() const;
102102
long long to_long_long() const;
103+
std::std::vector<uint64_t> to_base64();
103104

104105
// Random number generating functions:
105106
friend BigInt big_random(size_t);

include/constructors/constructors.hpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ BigInt::BigInt(const std::string& num) {
5353
if (num[0] == '+' or num[0] == '-') { // check for sign
5454
std::string num_magnitude = num.substr(1);
5555
if (is_valid_number(num_magnitude)) {
56-
/*
57-
TODO
58-
----
59-
magnitude = convert_to_base_2_to_the_64(num_magnitude);
60-
*/
61-
magnitude = {0};
56+
magnitude = convert_to_base_64(num_magnitude);
6257
is_negative = num[0] == '-';
6358
}
6459
else {
@@ -67,12 +62,8 @@ BigInt::BigInt(const std::string& num) {
6762
}
6863
else { // if no sign is specified
6964
if (is_valid_number(num)) {
70-
/*
71-
TODO
72-
----
73-
magnitude = convert_to_base_2_to_the_64(num_magnitude);
74-
*/
75-
magnitude = {0};
65+
std::string num_magnitude(num);
66+
magnitude = convert_to_base_64(num_magnitude);
7667
is_negative = false; // positive by default
7768
}
7869
else {

include/functions/conversion.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,20 @@ long long BigInt::to_long_long() const {
5858
return std::stoll(this->to_string());
5959
}
6060

61+
/*
62+
to_base64
63+
---------
64+
Convers a BigInt to its base64 representation
65+
*/
66+
67+
std::vector<uint64_t> BigInt::to_base64() {
68+
std::vector<uint64_t> magnitude;
69+
BigInt max_number("18446744073709551616"); // 2^64 for base conversion
70+
while (*this != 0) {
71+
magnitude.push_back(*this % max_number);
72+
*this /= max_number;
73+
}
74+
return magnitude;
75+
};
76+
6177
#endif // BIG_INT_CONVERSION_FUNCTIONS_HPP

include/functions/utility.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define BIG_INT_UTILITY_FUNCTIONS_HPP
99

1010
#include <tuple>
11+
#include <BigInt.hpp>
1112

1213

1314
/*

0 commit comments

Comments
 (0)