How to Convert Integer to Base7?


Given a integer, convert it to its base 7 representation in string format. For example, 101 = (202)7 and -7 = (-10)7

Recursive Approach to Convert Integer to Base7

We can break down the conversion using recursion. The Zero is an exceptional case where you would want to return the “0” directly. To convert negative numbers, we put a minus sign in front of its absolute value.

1
2
3
4
5
6
7
8
class Solution {
public:
    string convertToBase7(int num) {
        if (num == 0) return "0";
        if (num < 0) return "-" + convertToBase7(-num);
        return (num >= 7 ? convertToBase7(num / 7) : "") + (char)(48 + num % 7);
    }
};
class Solution {
public:
    string convertToBase7(int num) {
        if (num == 0) return "0";
        if (num < 0) return "-" + convertToBase7(-num);
        return (num >= 7 ? convertToBase7(num / 7) : "") + (char)(48 + num % 7);
    }
};

Iterative Conversion from Integer to Base7

We can iteratively divide the number by seven and put the remainder after modules % in the reverse order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
    string convertToBase7(int num) {
        if (num == 0) return "0";
        string r = "";
        string sgn = "";
        if (num < 0) {
            num = -num;
            sgn = "-";
        }
        while (num > 0) {
            r = std::to_string(num % 7) + r;
            num /= 7;
        }
        return sgn + r;
    }
};
class Solution {
public:
    string convertToBase7(int num) {
        if (num == 0) return "0";
        string r = "";
        string sgn = "";
        if (num < 0) {
            num = -num;
            sgn = "-";
        }
        while (num > 0) {
            r = std::to_string(num % 7) + r;
            num /= 7;
        }
        return sgn + r;
    }
};

Alternatively, if we append the remainder to the result string, we have to reverse it later.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
    string convertToBase7(int num) {
        if (num == 0) return "0";
        string r = "";
        string sgn = "";
        if (num < 0) {
            num = -num;
            sgn = "-";
        }
        while (num > 0) {
            r.push_back(48 + num % 7);
            num /= 7;
        }
        std::reverse(begin(r), end(r));
        return sgn + r;
    }
};
class Solution {
public:
    string convertToBase7(int num) {
        if (num == 0) return "0";
        string r = "";
        string sgn = "";
        if (num < 0) {
            num = -num;
            sgn = "-";
        }
        while (num > 0) {
            r.push_back(48 + num % 7);
            num /= 7;
        }
        std::reverse(begin(r), end(r));
        return sgn + r;
    }
};

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
269 words
Last Post: How to Construct String from Binary Tree?
Next Post: A Nice Alternative for PuTTY: Termius - A very nice portable SSH connection tool

The Permanent URL is: How to Convert Integer to Base7?

Leave a Reply