Skip to content

Commit

Permalink
Add convert_to_base_64 function to convert string integer to magnitudes
Browse files Browse the repository at this point in the history
  • Loading branch information
sahmad98 committed May 21, 2018
1 parent 20c8173 commit 783828f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ add_executable(Functions.Random.Test
test/functions/random.test.cpp)
target_link_libraries(Functions.Random.Test TestRunner)

add_executable(Utility.Helper.Test
test/functions/utility.test.cpp)
target_link_libraries(Utility.Helper.Test TestRunner)

# Operators:
add_executable(Operators.ArithmeticAssignment.Test
test/operators/arithmetic_assignment.test.cpp)
Expand Down
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);
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

0 comments on commit 783828f

Please sign in to comment.