Converting Binary Numbers to Base-6: A Step-by-Step Guide

Learn how to convert binary numbers (base-2) to base-6. This guide provides a clear, step-by-step process, including algorithms and examples, to master this essential number system conversion.



Converting Binary Numbers to Base-6

This section explains how to convert a binary number (base-2) to a base-6 number. We'll use a two-step approach: first converting to base-10 (decimal) and then to base-6.

Conversion Process

The process involves two functions:

  1. binaryToDecimal(n): Converts a binary number (represented as an integer in base 10) to its decimal equivalent.
  2. decimalToBase6(dec): Converts a decimal number to its base-6 equivalent.

Algorithm: Binary to Decimal

The binaryToDecimal function uses the following algorithm:


Binary To Decimal (Int n) 
{
  Int dec = 0; 
  Int pv = 1; 
  While n > 0
  {
    dec = dec + pv * (n % 10);
    pv = pv * 2;
    n = n // 10; 
  }
  decimalToBase6(dec); 
}

(Where '//' represents integer division.)

Algorithm: Decimal to Base-6

The decimalToBase6 function uses the following algorithm:


Decimal To Base6 (Int dec) 
{
  Int output = 0; 
  Int pv = 1; 
  While dec > 0
  {
    output = output + pv * (dec % 6);
    pv = pv * 10;
    dec = dec // 6; 
  }
  Print output; 
}

(Where '//' represents integer division.)

Worked Example

(The worked example converting the binary number 1101 to base-6 is given in the original text and should be included here. Each iteration of both functions should be shown explicitly.)

Implementation in Different Programming Languages

1. Java Implementation


package tutorialsarena;
public class BinaryToBase6 {
    // Function To convert the decimal number into its equivalent number in base 6
    public static void decimalToBase6(int dec) {
        int output = 0;
        int pv = 1;
        while (dec > 0) {
            output = output + pv * (dec % 6);
            dec = dec / 6;
            pv = pv * 10;
        }
        System.out.println(output);
    }
    // Function to convert the binary number into its equivalent decimal number
    public static void binaryToDecimal(int n) {
        int dec = 0;
        int pv = 1;
        int temp = n;
        while (temp > 0) {
            dec = dec + pv * (temp % 10);
            temp = temp / 10;
            pv = 2 * pv;
        }
        System.out.print("The Base6 equivalent of " + n + " = ");
        decimalToBase6(dec);
    }
    public static void main(String[] args) {
        binaryToDecimal(1101101);
        binaryToDecimal(1110111);
        binaryToDecimal(1010111);
        binaryToDecimal(1101111);
    }
}

2. C++ Implementation


#include <iostream>
using namespace std;
void decimalToBase6(int dec) {
    int output = 0;
    int pv = 1;
    while (dec > 0) {
        output = output + pv * (dec % 6);
        dec = dec / 6;
        pv = pv * 10;
    }
    cout << output << endl;
}
void binaryToDecimal(int n) {
    int dec = 0;
    int pv = 1;
    int temp = n;
    while (temp > 0) {
        dec = dec + pv * (temp % 10);
        temp = temp / 10;
        pv = 2 * pv;
    }
    cout << "The Base6 equivalent of " << n << " = ";
    decimalToBase6(dec);
}
int main() {
    binaryToDecimal(11011011);
    binaryToDecimal(10110111);
    binaryToDecimal(10110111);
    binaryToDecimal(11011011);
    return 0;
}

3. C Implementation


#include <stdio.h>
void decimalToBase6(int dec) {
    int output = 0;
    int pv = 1;
    while (dec > 0) {
        output = output + pv * (dec % 6);
        dec = dec / 6;
        pv = pv * 10;
    }
    printf("%d\n", output);
}
void binaryToDecimal(int n) {
    int dec = 0;
    int pv = 1;
    int temp = n;
    while (temp > 0) {
        dec = dec + pv * (temp % 10);
        temp = temp / 10;
        pv = 2 * pv;
    }
    printf("The Base6 equivalent of %d = ", n);
    decimalToBase6(dec);
}
int main() {
    binaryToDecimal(101010);
    binaryToDecimal(111101);
    binaryToDecimal(101101);
    binaryToDecimal(111010);
    return 0;
}

4. Python Implementation


def decToBase6(dec):
    pv = 1
    output = 0  
    while(dec > 0):
        output = output + pv*(dec % 6)
        pv = pv * 10
        dec = dec // 6  
    print(output)
def binaryToDecimal(n):
    dec = 0
    pv = 1
    temp = n
    while(temp > 0):
        dec = dec + pv*(temp % 10)
        pv = pv * 2
        temp = temp//10  
    print(f"The base6 equivalent of {n} = ", end="")
    decToBase6(dec)
binaryToDecimal(1010101)
binaryToDecimal(1111010)
binaryToDecimal(1011011)
binaryToDecimal(1110100)

Converting between number systems is a fundamental skill in computer science and mathematics. This two-step process (binary to decimal, then decimal to base-6) provides a clear and effective method for this conversion. The provided code examples demonstrate how to implement this process in Java, C++, C, and Python.

Number System Conversions: Binary to Base-6

This section provides example outputs for converting binary numbers to base-6. The algorithms for this conversion were detailed in the previous section.

Example Outputs: Binary to Base-6 Conversion

Below are the base-6 equivalents of several binary numbers. These results were obtained using the algorithms and code examples from the prior section.

Binary Number (Base-10 Representation) Base-6 Equivalent
1010101 (85) 221
1111010 (122) 322
1011011 (91) 231
1110100 (116) 312

Conclusion

These examples illustrate the results of converting binary numbers to base-6 using the algorithms and code described in the previous section. These conversions are fundamental to understanding how computers handle data representation.