# Convert a number from base 2 to base 6

### Base 2

Base 2 is the format where you can represent any number with just two values which are 0 and 1. With the help of 0s and 1s, we can represent any number we want. It is also called Binary format.

### Base6

Base 6 is the format where we can use 6 digits (0 to 5) to represent any number. Base 6 is generally not used, while base 2 (binary) is used in computers and complete electronics.

In real life, we deal with the base 10 format, which is also called decimal format, where to represent any number; we use 10 digits (0 to 9).

To convert any number from base 2 to base 6, we will follow the following steps:

1. Convert the number into base 10 (decimal) from base 2(binary)

2. Now convert the number from base 10 to base 6.

**For example:**

We have a base 2 number N=(1100101)_{2}, and our task is to convert this N into base 6.

**Step1: **

From LSB (Least Significant Bit), add each number, and it’s multiplied by 2 to the power of its index.

N In decimal would be: (1X26)+(1X25)+(0X24)+(0X23)+(1X22)+(0X21)+(1X20)

So N = (101)_{10}

**Step2:**

Now convert this 101 into an equivalent base 6 number by the dividing method.

ans=0

101%6=5 so add 5 into ans in LSB ,Now ans= 5

Value => 101/6=16

Now 16%6=4 so ad d4 into ans in LSB, Now ans=45

value=> 16/6=2

Since the value is lesser than 5, just add into ans in lSB and stop so ans=245

So finally, our base 6 would be = (245).

We will see the JAVA code for the following conversion:

**Java Code:**

```
class HelloWorld {
public static void main(String[] args) {
System.out.println(base10Tobase6(base2Tobase10("1100101")));
}
public static int base2Tobase10(String binary){
int n=binary.length();
int ans=0;
for(int i=0;i<n;i++){
char ch=binary.charAt(i);
if(ch=='1')
ans+=Math.pow(2,n-i-1);
}
return ans;
}
public static String base10Tobase6(int decimal){
int ans=0;
int idx=0;
while(decimal>0){
ans+=(decimal%6)*Math.pow(6,idx++);
decimal/=6;
}
return ans+"";
}
}
```

If we discuss the time complexity and space complexity, then we will notice that the first function, which converts the N length string of binary numbers into an equivalent decimal number, has a time complexity of O(N). and the second function, which converts the decimal integer to base 6 string, has the time complexity of O(M), where M is the length of the bits in base 6 format. For any number, N will always be greater than and equal to M.

So finally, time complexity would be: O(N).

Space taken by the first function is O(1), as it takes only one integer variable to return its answer. The second function takes the O(M) space of the string to store the result of the base 6 format.

So space complexity would be: O(M).

We can reverse the above steps to convert the number from base 2 to base 6.

**Step1:** convert from base 6 to base 10

**Step2:** convert from base 10 to base 2

**Example:** 245 we can convert it into decimal which will be

2X62+4X61+5X60, so the decimal equivalent will be equal to 101.

Step2 is to convert 101 to binary using the divide method by 2.

value=101 ans=””.

ans=(101)%2 ans value=101/2 so value=50 and ans=1

ans=50%2 in MSB and value = 50/2 , so value=25 and ans=01

ans=25%2 in MSB and value = 25/2 , so value=12 and ans=101

ans=12%2 in MSB and value = 12/2 , so value=6 and ans=0101

ans=6%2 in MSB and value = 6/2 , so value=3 and ans=00101

ans=3%2 in MSB and value = 3/2 , so value=1 and ans=100101

ans=1%2 in MSB and value = 1/2 , so value=0 and ans=1100101

As the value becomes 0 so, we will stop, and 1100101 is binary equivalent to 245 of base 6.

**Java Code:**

```
class HelloWorld {
public static void main(String[] args) {
System.out.println(base10Tobase2(base6Tobase10("245")));
}
public static int base6Tobase10(String base6){
int n=base6.length();
int ans=0;
for(int i=0;i<n;i++){
char ch=base6.charAt(i);
ans+=(ch-'0')*Math.pow(6,n-i-1);
}
return ans;
}
public static String base10Tobase2(int decimal){
String ans="";
while(decimal>0){
ans=(decimal%2)+ans;
decimal/=2;
}
return ans;
}
}
```

If we discuss the time complexity and space complexity, then we will notice that the first function, which converts the N length string of base 6 numbers into an equivalent decimal number, has a time complexity of O(M). The second function, which converts the decimal integer to a base 2 string, has the time complexity of O(N), where M is the length of the bits in base 6 format. For any number, N will always be greater than and equal to M.

So finally, time complexity would be: O(N).

Space taken by the first function is O(1), as it takes only one integer variable to return its answer. The second function takes the O(N) space of the string to store the result of the base 2 format.

So space complexity would be: O(N).