Dan Chisholm
Hex, Octal, Binary, Decimal


Home
Tutorials
Fundamentals

To convert a binary byte to octal just divide the byte into three nibbles. The first nibble has two bits and the next two nibbles each have three. For example, convert the binary value 01010011 to octal.

Binary 01 010 011
Octal   1   2   3

To convert a binary value to hexadecimal just divide the byte into two nibbles of four bits each.

Binary 0111 1111
Hex       7    f

The following table demonstrates a set of conversions.

Base Conversion Table
Decimal Binary Octal Binary Hexadecimal
000 000 00000000 00000
100 000 00110000 00011
200 000 01020000 00102
300 000 01130000 00113
400 000 10040000 01004
500 000 10150000 01015
600 000 11060000 01106
700 000 11170000 01117
800 001 000100000 10008
900 001 001110000 10019
1000 001 010120000 1010a
1100 001 011130000 1011b
1200 001 100140000 1100c
1300 001 101150000 1101d
1400 001 110160000 1110e
1500 001 111170000 1111f
1600 010 000200001 000010
2600 011 010320001 10101a
3600 100 100440010 010024
4600 101 110560010 11102e
5600 111 000700011 100038
6601 000 0101020100 001042
7601 001 1001140100 11004c
8601 010 1101260101 011056
9601 100 0001400110 000060
10601 101 0101520110 10106a
11601 110 1001640111 010074
12601 111 1101760111 11107e


Here's an example question that prints Byte.MAX_VALUE in binary, octal, decimal, and hex.

Question 9

class A {
  public static void main(String args[]) {
    System.out.print(Integer.toBinaryString(Byte.MAX_VALUE)+",");
    System.out.print(Integer.toOctalString(Byte.MAX_VALUE)+",");
    System.out.print(Integer.toString(Byte.MAX_VALUE)+",");
    System.out.print(Integer.toHexString(Byte.MAX_VALUE));
  }
}


What is the result of attempting to compile and run the program?

a.  Prints: 1111111,177,127,7f
b.  Prints: 11111111,377,256,ff
c.  Compiler Error
d.  Runtime Error
e.  None of the Above

The binary representation of Byte.MAX_VALUE is as follows.

01111111

The Integer.toBinaryString method does not print the leading zero. Instead, it only prints the seven bits that are set to one as follows.

1111111

To convert the binary byte to octal just divide the byte into three nibbles.

Binary 01 111 111
Octal   1   7   7

To convert Byte.MAX_VALUE to hexadecimal just divide the byte into two four bit nibbles.

Binary 0111 1111
Hex       7    f

To convert Byte.MAX_VALUE to decimal raise 2 to the seventh power and then subtract one. The result is 127.


Copyright © 2002, Dan Chisholm
All rights reserved.