VOOZH about

URL: https://www.geeksforgeeks.org/dsa/convert-a-number-from-base-2-to-base-6/

⇱ Convert a number from base 2 to base 6 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convert a number from base 2 to base 6

Last Updated : 28 Aug, 2025

Given a binary integer N, the task is to convert it into base 6.

Note: The number of bits in N is up to 100. 

Examples:

Input: N = "100111"
Output: 103
Explanation: The given integer (100111)2 is equivalent to (103)6.

Input: N = "1111111"
Output: 331

Approach: The given problem can be solved by first converting the given integer to decimal, thereafter converting the number from decimal to the base 6 using an approach discussed here. Please note that since the value of the N can reach up to 2100, the 128-bit integer can be used to store the decimal number. 

Below is the implementation of the above approach:


Output
103

Time Complexity: O(len(N))
Auxiliary Space: O(1)

Another Approach: The given problem can also be solved by maintaining the integer in base 6 in place of the decimal conversion while converting the base of the binary integer to decimal. It is known that the binary number can be converted to a decimal using the following steps:

N = "1001"
N can be converted to (N)10 with the equation: (((1*2 + 0) *2 + 0) *2) + 1).

Hence, two types of steps are required, multiplying the integer by 2 which is equivalent to adding the integer in itself, and adding 0 or 1 to the integer as (0, 1)2 is equivalent to (0, 1)6. Hence, maintain a string representing a base 6 integer, and in each step, add the integer to itself and add 0 or 1 accordingly in each step. If can be done using the approach discussed here

Below is the implementation of the above approach:



Output
103


Time Complexity: O(len(N)2)
Auxiliary Space: O(len(N))

Note that the complexity of 1st approach is less than the second approach but the first approach can only handle binary integers up to 127 bits while the second approach can handle much larger values as well. 

Comment
Article Tags: