Skip to content

Commit 522f8d7

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

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ add_executable(Functions.Random.Test
4949
test/functions/random.test.cpp)
5050
target_link_libraries(Functions.Random.Test TestRunner)
5151

52+
add_executable(Utility.Helper.Test
53+
test/functions/utility.test.cpp)
54+
target_link_libraries(Utility.Helper.Test TestRunner)
55+
5256
# Operators:
5357
add_executable(Operators.ArithmeticAssignment.Test
5458
test/operators/arithmetic_assignment.test.cpp)

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/utility.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,30 @@ bool is_power_of_10(const std::string& num){
110110
return true; // first digit is 1 and the following digits are all 0
111111
}
112112

113+
114+
/*
115+
conver_to_base_64
116+
----------------------
117+
Converts a string represented BigInt into the Base64 magnitudes
118+
*/
119+
120+
std::vector<unsigned long long> convert_to_base_64(std::string& num) {
121+
std::vector<unsigned long long> magnitude;
122+
auto start_it = num.size() - 1; // Starting from Backward direction
123+
auto stop_it = num.size();
124+
uint64_t component = 0;
125+
while(start_it != -1) {
126+
try {
127+
std::string num_slice = num.substr(start_it, stop_it);
128+
component = std::stoull(num_slice);
129+
} catch (std::out_of_range& e) { //Using out_of_range to determine overflow
130+
magnitude.push_back(component);
131+
stop_it = start_it + 1;
132+
}
133+
--start_it;
134+
}
135+
magnitude.push_back(component);
136+
return magnitude;
137+
}
138+
113139
#endif // BIG_INT_UTILITY_FUNCTIONS_HPP

test/functions/utility.test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <string>
2+
#include <vector>
3+
4+
#include "functions/utility.hpp"
5+
6+
#include "third_party/catch.hpp"
7+
8+
TEST_CASE("Conversion to Base64 magnitudes from String", "[Utility][Helper][Base64]") {
9+
std::string num = "481482486096808911670147153572883801";
10+
// Magnitude should be {11670147153572883801, 4814824860968089}
11+
auto magnitude = convert_to_base_64(num);
12+
REQUIRE(magnitude.size() == 2);
13+
REQUIRE(magnitude[0] == uint64_t(11670147153572883801));
14+
REQUIRE(magnitude[1] == uint64_t(4814824860968089));
15+
16+
num = "1";
17+
magnitude = convert_to_base_64(num);
18+
REQUIRE(magnitude.size() == 1);
19+
REQUIRE(magnitude[0] == uint64_t(1));
20+
21+
num = "";
22+
magnitude = convert_to_base_64(num);
23+
REQUIRE(magnitude.size() == 1);
24+
REQUIRE(magnitude[0] == uint64_t(0));
25+
}

0 commit comments

Comments
 (0)