WROTE IN AIRPLANE: Checked edge case where the number is zero

master
Aadhavan Srinivasan 10 months ago
parent 52f8034f4e
commit dd658c9c1d

@ -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 from_decimal(unsigned int num, int to_base) {
std::string return_val; std::string return_val;
int val = 0; int val = 0;
while (num > 0) { /* Handle the special case of num being zero: In this case, the result is also zero */
val = num % to_base; if (num == 0) {
return_val.push_back(possible_chars[val]); return_val = "0";
num /= to_base; } 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 */ /* Reverse the string, since we started from the right */
std::reverse(return_val.begin(), return_val.end()); std::reverse(return_val.begin(), return_val.end());

Loading…
Cancel
Save