Hexadecimal Numbers

Cards (35)

  • Hexadecimal (hex) is another number system used regularly in programming
  • Hex uses a combination of digits and letters in order to represent a number
  • Hexadecimal numbers are shorter than binary
  • Hexadecimal (or base-16) uses sixteen different digits
  • A single hex character can represent any denary number from 0 - 15
  • To represent 0-15 in binary would require 4 bits (a nibble), so each hex character equals to a nibble in binary
  • Programmers often prefer hex when coding because:
    • It's simpler to remember large numbers in hex - they're far shorter than binary numbers
    • Due to hex numbers being shorter, there's less chance of input errors
    • It's easier to convert between binary and hex than binary and denary
  • Computers themselves don't use hex - they still have to convert everything to binary to process it
  • Denary - hex - binary
    0 - 0 - 0000
  • Denary - hex - binary
    1 - 1 - 0001
  • Denary - hex - binary
    2 - 2 - 0010
  • Denary - hex - binary
    3 - 3 - 0011
  • Denary - hex - binary
    4 - 4 - 0100
  • Denary - hex - binary
    5 - 5 - 0101
  • Denary - hex - binary
    6 - 6 - 0110
  • Denary - hex - binary
    7 - 7 - 0111
  • Denary - hex - binary
    8 - 8 - 1000
  • Denary - hex - binary
    9 - 9 - 1001
  • Denary - hex - binary
    10 - A - 1010
  • Denary - hex - binary
    11 - B - 1011
  • Denary - hex - binary
    12 - C - 1100
  • Denary - hex - binary
    13 - D - 1101
  • Denary - hex - binary
    14 - E - 1110
  • Denary - hex - binary
    15 - F - 1111
  • Convert hex to denary by multiplying each character
  • In hex, moving right to left, place values increase in powers of 16: 4096 - 256 - 16 - 1
  • Example
    Convert the hexadecimal 87 into denary
    • First draw a table with 2 columns and 2 rows (16 and 1 across the top / hex on the bottom)
    16 1
    8 7
    • Then write the hex number and multiply the numbers in each column
    > 8 x 16 = 128 7 x 1 = 7
    • Add up the results
    > 128 + 7 = 135
    = So the hex number 87 is 135 in denary
  • Example
    Convert the denary number 106 into hexadecimal
    • Draw this table
    16 1
    6 A
    • Start at the left. Divide 106 by 16, then hold onto the remainder
    > 106 / 16 = 6 r 10
    • Divide the remainder from the last calculation by 1
    > 10 / 1 = 10 = A
    = So the denary number 106 is 6A in hex
  • Convert binary in hex by splitting it into nibbles
  • Each hex character is equal to a nibble in binary, so it is possible to convert from binary to hex by splitting the binary code in 4-bit chunks
  • Binary to hex conversions can be much easier than converting from binary to denary, as you only have to deal with the nibbles one at a time
  • Example
    Convert the binary number 10111001 to hex
    • Firstly split the binary number into nibbles: 1011 1001
    • Draw a table with columns labelled 8, 4, 2, 1, then repreat the values for as many nibbles as you require
    8 4 2 1 / 8 4 2 1
    • Fill in the table with your binary number
    8 4 2 1 / 8 4 2 1
    1 0 1 1 / 1 0 0 1
    • For each nibble, add up the numbers with 1 in the column, then convert the value to hex and put the values together
    > The binary number 10111001 is B9 in hex
  • When converting binary to hex, if the number can't be split into nibbles, you'll have to put some zeros on the front
  • Example
    Convert the binary number 11,110 to hex
    • Add 0s to the front of the binary number, so that you can split it into nibbles
    0 0 1 1 / 1 1 1 0
    • Draw a repeating table of 8, 4, 2, 1
    8 4 2 1 / 8 4 2 1
    • Write your binary number in the table
    8 4 2 1 / 8 4 2 1
    0 0 1 1 / 1 1 1 0
    • Add up each nibble and convert each value to hex
    > 2 + 1 = 3 8 + 4 + 2 = 14 = E
    > The binary number 11,110 in 3E in hex
  • Example
    Convert the hexadecimal number 8C to binary
    • Find the denary value of each character
    8 4 2 1 / 8 4 2 1
    1 0 0 0 / 1 1 0 0
    • Find the binary value of each number
    • Put the nibbles together to get the equivalent binary number
    > The hexidecimal number 8C is 10001100 in binary