Used unsigned int instead of int when converting to base-10

master
Aadhavan Srinivasan 8 months ago
parent 24c1dd6391
commit 2c735896df

@ -3,10 +3,10 @@
#include <string>
/* Convert the given value from the given base, to base 10 */
int to_decimal(std::string num, int from_base);
unsigned int to_decimal(std::string num, int from_base);
/* Convert the given value from base 10 to the given base */
std::string from_decimal(int num, int to_base);
std::string from_decimal(unsigned int num, int to_base);
/* Convert the given value from 'from_base', to 'to_base' */
std::string base_convert(std::string num, int from_base, int to_base);

@ -5,10 +5,10 @@
std::string possible_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int to_decimal(std::string num, int from_base) {
unsigned int to_decimal(std::string num, int from_base) {
char current_char = 0;
int index = 0;
int value = 0;
unsigned int value = 0;
/* Here, we convert 'num' to decimal (base 10) - Find the index of
every character in the string, in 'possible_chars' and
@ -23,7 +23,7 @@ int to_decimal(std::string num, int from_base) {
}
/* Convert the given value from base 10 to the given base */
std::string from_decimal(int num, int to_base) {
std::string from_decimal(unsigned int num, int to_base) {
std::string return_val;
int val = 0;
while (num > 0) {
@ -40,7 +40,7 @@ std::string from_decimal(int num, int to_base) {
/* Convert the given value from 'from_base', to 'to_base' */
std::string base_convert(std::string num, int from_base, int to_base) {
int temp = to_decimal(num, from_base);
unsigned int temp = to_decimal(num, from_base);
std::string result = from_decimal(temp, to_base);
return result;

Loading…
Cancel
Save