×

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).


Related Topics

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...

4 minutes read.

CPU Registers

CPU Registers Within the CPU, we have small- small storage units called registers. If we are executing a certain program within the CPU, there's a need to store that program content,...

11 minutes read.

Digital Number System

Digital Number System: The computer understands digital language only. Many number systems are used in digital technology. The most common are Decimal, Binary, Octal and Hexadecimal number systems. Types of the...

7 minutes read.

Fixed and Floating-Point Number

Fixed and Floating-Point Number: In digital technology, data is stored in memory registers with binary bits 0’s and 1’s because the computer only understands binary language. When we enter data...

6 minutes read.

Bipartite Graph

A graph is said to be bipartite if and only if we can divide all the vertices of a graph into two sets such that these sets are mutually exclusive...

4 minutes read.

Flag Register of 8086 Microprocessor

In this article, we are going to study about the one of the registers named flag registers of 8086 microprocessor. We are also going to know where and how to...

3 minutes read.

Use Case Diagram for the online bank system

What is a Use Case Diagram? A tool is called a Use Case Diagram which is used to create, present, and comprehend the functional requirements for a system. The Use Case...

3 minutes read.

Micro-Operations

Micro-Operations If CPU wants to perform any operation, suppose execution of only one instruction or even break it down one particular execution phase (like fetch instruction, write back, decode, operand fetch...

9 minutes read.

Data Transfer and Manipulation

What is Data Transfer? In computer architecture, data transfer refers to the movement of data between different components within a computer system. These components can include the central processing unit (CPU),...

6 minutes read.

Local Broadcast Address and loopback address

In computer networking, if you send the data from one source to another source or group of sources in the form of packets, then this is called casting. We can categorize...

3 minutes read.

Types of Microprocessors

Introduction:Microprocessor is known as the computer processor. The IC or integrated Circuit makes the microprocessor. That is the brain of the computer machine.  The microprocessor is the Processor or CPU (Central...

3 minutes read.

Instruction Pipelining and Pipeline Hazards

Instruction Pipelining and Pipeline Hazards To apply pipeline, the prerequisite is to perform the same processing over multiple inputs, then pipelining is important. So instruction execution is the best example for...

10 minutes read.

Basic Terminologies Related to COA

Some Basic Terminologies Related to COA Computer organization and architecture is the detailed study of the internal components of the computer with their functionality. It describes how components arranged together and...

7 minutes read.

Assembly Language Register

Assembly Language Register Assembly language was introduced during the late 1940s in the second generation. It is a low-level language that helps in communication between the user and the computer. It...

6 minutes read.

8086 Microprocessor Pin Configurations

8086 microprocessor is invented and developed by Intel in 1976. The 8086 Microprocessor is an enhanced version of the 8085 Microprocessor. There are 20 address lines and 16 data lines...

6 minutes read.

Microprocessor classification

Introduction: Microprocessor is known as the computer processor. The IC or integrated Circuit makes the microprocessor. that is the brain of the computer machine. The microprocessor is known as the...

6 minutes read.

8085 Pin Configuration

Introduction: Intel 8085 is consist 40 pin IC. The pin name of 8085 is A8 – A15; AD0 – AD7; ALE; IO/M; S0, S1; RD; WR; READY; HOLD; HLDA; INTR; INTA;...

6 minutes read.

Computer Organization and Architecture Data Formats

Data Formats In the digital world, everything is based on digits. In every method, the calculation is processed on the digits, and most importantly, the computer only understands the binary language...

6 minutes read.

Control Signals in 8155 Microprocessor

Intel designed the chip 8155 used to interface with I/O devices. Basically, the 8085 microprocessor is directly unable to deal with its peripheral input output devices for reading data or...

3 minutes read.

Instruction Cycle: Computer Organization and Architecture

What is an effective Address? “Effective address is the address of operand in a computation-type instruction.” Computation type instructions are like ADD, SUB, MOV, OR, AND, etc., which require operand to...

4 minutes read.