🔢

Base Conversions

TypeQuiz 1 Material

Notes

Conversions

Binary to Decimal

Online Converter

Visual Representation:

DecimalBinaryArithmetic
000000+0+0+0
100010+0+0+1
200100+0+2+0
300110+0+2+1
401000+4+0+0
501010+4+0+1
601100+4+2+0
701110+4+2+1
810008+0+0+0
910018+0+0+1
1010108+0+2+0
1110118+0+2+1
1211008+4+0+0
1311018+4+0+1
1411108+4+2+0
1511118+4+2+1

0b010101110 is sometimes used for binary, but is not standard in Java and C

// Does not include negative numbers
public static int binaryStringToInt(String binary) {
    int output = 0;
    for (int i = 0; i < binary.length(); i++) {
        if (binary.charAt(i) == '1') {
            output += 1 << binary.length() - i - 1;
        }
    }
    return output;
}

Binary to Octal

Online Converter

Diagram:

Base 2000111010100101
Base 807245
public static String binaryStringToOctalString(String binary) {
    int decimal = 0;
    for (int i = 0; i < binary.length(); i++) {
        if (binary.charAt(i) == '1') {
            decimal += 1 << binary.length() - i - 1;
        }
    }

    String output = "";
    int counter = 21;
    for (int i = 7; i >= 0; --i) {
        output += (decimal >> counter) & 0b111;
        counter -= 3;
    }
    return output;

Binary to Hexadecimal

Online Converter

Diagram:

Base 210001111001111000001
Base 88F3C1

Decimal to Binary

Online Converter

Decimal to Octal

Online Converter

Decimal to Hexadecimal

Online Converter

public static String intToHexString(int hex) {
    String output = "";
    int counter = 16;
    while (counter <= hex) {
        counter = counter << 4;
    }
    counter = counter >> 4;
    int counter2 = 0;
    while (counter != 0) {
        if (hex < counter) {
            if (counter2 > 9) {
                output += (char)(counter2 + 65 - 10);
            } else {
                output += counter2;
            }
            counter = counter >> 4;
            counter2 = 0;
        }
        hex -= counter;
        counter2++;
    }
    return output;
}

Octal to Binary

Online Converter

Base 864302
Base 2110100011000010

0456 is octal

Octal to Decimal

Online Converter

public static int octalStringToInt(String octal) {
     int output = 0, counter = 0;
     for (int i = octal.length() - 1; i > -1; i--) {
         output += (octal.charAt(i) - 48) << counter;
         counter += 3;
     }
     return output;
 }

Octal to Hexadecimal

Online Converter


Hexadecimal to Binary

Online Converter

Diagram Example:

Base 16F00D5
Base 211110000000011010101

0x456 is hexadecimal

Hexadecimal to Decimal

Online Converter

Hexadecimal to Octal

Online Converter


Bonus Info

Converting base 10 string to int:

public static int decimalStringToInt(String decimal) {
    int output = 0, counter = 1;
    for (int i = decimal.length() - 1; i > -1; i--) {
        output += (decimal.charAt(i) - 48) * counter;
        counter *= 10;
    }
    return output;
}

Powers of 2

2^nliteral value
2^01
2^12
2^24
2^38
2^416
2^532
2^664
2^7128
2^8256
2^9512
2^101024
2^112048
2^124096
2^138192
2^1416384
2^1532768
2^1665536

Questions & Answers

How many different numbers can be represented by n bits?

2^n

What are the tricks to doing conversions in between binary/octal/hexadecimal (larger numbers) quickly?