Loading

C++: Binary, Octal, and Hexadecimal

Binary, Octal, and Hexadecimal

In this C++ video tutorial, we demonstrate various numerical representations using binary, octal, and hexadecimal. Binary, which is base 2, is inconvenient to use because it requires over three times as many digits as ordinary decimal numbers to encode it; for example, 78 in decimal is written as 1001110 in binary. For this reason, programmers prefer to use number bases with larger powers of 2, like 8 and 16--called octal and hexadecimal, respectively.

Binary

In the video, we presented code to output int data types in binary, octal, and hexadecimal. The functions that we wrote gave the numbers as 32 bit data, broken down into digits according to the corresponding number bases.

ConversionsOutput

Octal representations encode three bits per digit. Above, we have an octal representation with its digits underneath the corresponding bits of the binary representation. Below, we have a conversion table between octal and binary; each possible octal digit is shown next to its three bit conversion.

Octal Table

Hexadecimal representations encode four bits per digit. In the program output above, we also have a hexadecimal representation with its digits shown underneath the corresponding bits of the binary representation. Below, we have a conversion table between hexadecimal and binary; each possible hexadecimal digit is shown next to its four bit conversion. Notice that the digits corresponding to 10 through 15 have been assigned letter values.

Hexadecimal Table

For octal, hexadecimal, and ordinary decimal number bases, we can format the stream objects with three letter abbreviations: oct, hex, and dec. These allow us to output numbers in the respective formats without writing functions. However, this built-in formatting removes all of the leading zeroes.

In the program below, we demonstrate how to output integers in octal, decimal, and hexadecimal. We begin by declaring an array of ints and assigning the entries the values of six literals given in octal, decimal, and hexadecimal. We note that the compiler allows us to use either uppercase or lowercase letters in our hexadecimal representations. After the assignments, we loop over the array of six entries and output each one in octal, decimal, and hexadecimal.

#include <iostream>

int main() {
	using namespace std;

	int iArray[6];
	// Assign Octal literals
	iArray[0] = 0734;
	iArray[1] = 03616;
	// Assign Decimal literals
	iArray[2] = 47961;
	iArray[3] = 658;
	// Assign Hexadecimal literals
	iArray[4] = 0xF62D;
	iArray[5] = 0xCB38;

	// Loop over the array entries
	for (int iIndex = 0; iIndex < 6; ++iIndex) {
		cout << "Oct = " << oct << iArray[iIndex] << endl;
		cout << "Hex = " << hex << iArray[iIndex] << endl;
		cout << "Dec = " << dec << iArray[iIndex] << endl;
		cout << endl;
	}

	// Keep the window open
	cin.get();
	return 0;
}

The output of the program is shown below. We remark that we need to prepend a '0' or '0x' in order to signal to the compiler that a literal is in octal or hexadecimal, respectively. Compare the literal assignments to the output values to see how each number is converted.

Program Output