diff --git a/numeric_base.cpp b/numeric_base.cpp index 309265f..389fa72 100644 --- a/numeric_base.cpp +++ b/numeric_base.cpp @@ -26,12 +26,17 @@ unsigned int to_decimal(std::string num, int from_base) { std::string from_decimal(unsigned int num, int to_base) { std::string return_val; int val = 0; - while (num > 0) { - val = num % to_base; - return_val.push_back(possible_chars[val]); - num /= to_base; + /* Handle the special case of num being zero: In this case, the result is also zero */ + if (num == 0) { + return_val = "0"; + } else { + while (num > 0) { + val = num % to_base; + return_val.push_back(possible_chars[val]); + num /= to_base; + } } - + /* Reverse the string, since we started from the right */ std::reverse(return_val.begin(), return_val.end());