DoAssignment study guide

A1.2 · Explain binary, hexadecimal, and character encoding

Learn to explain binary, hexadecimal, and character encoding through clear examples and targeted practice.

Ontario Grade 11 Computer Science

Programming Concepts and Skills

How number systems and shared rules represent information

Computers store information as patterns of bits, each with a value of 0 or 1. A pattern can be read as a number, or interpreted as text when a character-encoding rule is used. Binary and hexadecimal are ways to write number values; character encoding gives rules for connecting characters with numeric codes and stored bytes. In this lesson, you will learn how these representations relate—and why they are not interchangeable.

What you will learn

  • Explain how binary place values represent whole numbers.
  • Convert binary values to hexadecimal by grouping bits in fours.
  • Explain how character encodings connect characters with numeric codes and bytes.

1. Binary: numbers with two digits

Our familiar number system is decimal, or base 10. Its place values are ones, tens, hundreds, and so on. Binary is base 2, so each place is twice the value of the place to its right: ones, twos, fours, eights, sixteens, and thirty-twos. A bit of 1 includes its place value; a bit of 0 leaves it out.
In binary 101101, the 1 bits select the thirty-twos, eights, fours, and ones places. Adding those values gives decimal 45. The bit pattern is a compact way of showing which place values contribute to the total.
A numeral's digits alone do not identify its base. For example, 101 means five in binary but one hundred and one in decimal. State the base whenever the context is unclear.
1011012=32+8+4+1=4510101101_2=32+8+4+1=45_{10}
  • Binary is base 2 and uses only 0 and 1.
  • From right to left, binary place values are 1, 2, 4, 8, 16, and so on.
  • To find a binary numeral's decimal value, add the place values marked by 1.

2. Hexadecimal: a compact way to write values

Hexadecimal, often shortened to hex, is base 16. Its symbols are 0 through 9, followed by A, B, C, D, E, and F for the values ten through fifteen. Each hex place is sixteen times the value of the place to its right.
One hex digit corresponds to four binary bits. To convert binary to hex, group the bits into fours starting from the right. If the leftmost group has fewer than four bits, add zeroes to its left; this padding does not change the value. Then replace each group with its matching hex digit.
For example, 1010 has value eight plus two, or ten. The hex symbol for ten is A. Hexadecimal is a shorter way to write the same number value; it does not change the number.
10102=1010=A161010_2=10_{10}=A_{16}
  • Hex is base 16 and uses 0–9 and A–F.
  • Each hex digit represents one group of four bits.
  • Group binary bits from the right; pad a short leftmost group with zeroes.

3. Character encoding: rules for representing text

A bit pattern does not inherently mean a particular letter. A character encoding is a rule for associating characters with numeric codes represented in stored data as bits and bytes. A byte is a group of eight bits. To interpret stored bytes as text, a reader needs a compatible encoding.
ASCII assigns codes to a limited set of characters, including English letters, digits, and punctuation. Uppercase A has decimal code 65, which can also be written as binary 01000001 or hexadecimal 41. ASCII does not cover every character used in the world's writing systems.
Unicode assigns code points to characters from many writing systems. UTF-8 is a way to encode Unicode text as bytes. It uses one or more bytes for a character. Characters in the ASCII set keep their familiar one-byte values, while some other characters need multiple bytes. Thus, one character is not necessarily one byte.
The Python 3 example below takes a string as input, encodes it as UTF-8, and displays the resulting bytes in hexadecimal. The UTF-8 bytes for é are C3 and A9, so the hexadecimal display is c3a9. Python is one classroom language; other classrooms may use different languages and syntax. This code is shown for tracing, not execution.
6510=010000012=411665_{10}=01000001_2=41_{16}
  • Character-encoding rules connect characters with numeric codes or byte patterns.
  • ASCII covers a limited set; Unicode assigns code points across many writing systems.
  • UTF-8 encodes Unicode text as bytes, using multiple bytes for some characters.

4. Keep the representations distinct

Binary and hexadecimal are ways to write numeric values. An encoding supplies rules for interpreting values or bytes as characters. For example, hexadecimal 41 is the byte value associated with uppercase A in ASCII and UTF-8. The digits 41, the byte value, and the character A are related, but they are not the same kind of thing.
When you encounter a pattern, first ask whether it is being presented as a number or as encoded text. For a number, identify its base. For text, identify the encoding used to interpret the bytes. The same stored value can have different meanings in different contexts.
  • Binary and hex are different notations for number values.
  • An encoding is needed to interpret bytes as text.
  • A character can be represented by more than one byte.

Trace of the UTF-8 example

StageValue
Input texté
After UTF-8 encodingbytes C3 A9
After hexadecimal displayc3a9

Worked example

Read a binary number

Find the decimal value of binary 101101 by adding the place values marked by 1.
Bits and place values in 101101
Bits (most significant to least significant)32116081412011

Each cell pairs a bit with its binary place value.

  1. Match bits to places
    Starting at the right, match the six bits to place values 1, 2, 4, 8, 16, and 32. Include only the values whose matching bit is 1.
    1011012=32+8+4+1101101_2=32+8+4+1
  2. Add the selected values
    The zero bits leave out the 16 and 2 places. Adding the selected values gives the decimal result.
    32+8+4+1=4532+8+4+1=45
Answer: Binary 101101 represents decimal 45.
Check: The place values from left to right are 32, 16, 8, 4, 2, and 1. The bits marked 1 select 32, 8, 4, and 1.

Worked example

Convert binary to hexadecimal

Write binary 11010110 in hexadecimal by grouping the bits into fours.
Four-bit groups in 11010110
Bits (most significant to least significant)group 1, bit 11group 1, bit 21group 1, bit 30group 1, bit 41group 2, bit 10group 2, bit 21group 2, bit 31group 2, bit 40

The first four bits make D; the second four bits make 6.

  1. Group the bits
    Starting from the right, divide the eight bits into two groups of four. No padding is needed because both groups already contain four bits.
    1101 01101101\ 0110
  2. Convert each group
    The first group has value thirteen, represented by D in hexadecimal. The second group has value six, represented by 6.
    11012=D16,01102=6161101_2=D_{16},\quad 0110_2=6_{16}
Answer: Binary 11010110 is D6 in hexadecimal.
Check: D6 has value thirteen sixteens plus six, or 214. The binary groups represent thirteen sixteens plus six as well.

Worked example

Trace UTF-8 bytes for a character

For the Python 3 input string é, identify the hexadecimal byte display produced by the shown code.
UTF-8 byte bits for é
Bits (most significant to least significant)C3 bit 11C3 bit 21C3 bit 30C3 bit 40C3 bit 50C3 bit 60C3 bit 71C3 bit 81A9 bit 11A9 bit 20

The first eight bits form byte C3; the next eight form byte A9.

Python 3

text = "é"
encoded = text.encode("utf-8")
print(encoded.hex())
  1. Identify the input and encoding
    The input is a string containing the single character é. The code requests UTF-8 encoding; under that rule, this character is represented by byte values C3 and A9.
    eˊ↦C3 A916\text{é}\mapsto\text{C3 A9}_{16}
  2. Trace the hexadecimal display
    After encoding, encoded holds the bytes for C3 A9. The hex method returns their hexadecimal digits as the string c3a9, and print displays that string. The code does not print the bytes object's representation.
    C3 A916↦c3a9\text{C3 A9}_{16}\mapsto\text{c3a9}
Answer: The expected printed output is c3a9.
Check: The input has one character, but its UTF-8 representation has two bytes. The displayed digits correspond to those byte values.

Common mistakes and how to avoid them

Using decimal place values to read a binary numeral.
Correction: Use powers of two for its positions and add only the places marked by 1.
Treating one hex digit as one bit.
Correction: One hex digit represents four bits; for example, A corresponds to 1010.
Assuming every character takes exactly one byte.
Correction: The number of bytes depends on the character and encoding. UTF-8 uses multiple bytes for some characters.
Assuming a byte pattern identifies text by itself.
Correction: Use the appropriate character encoding to interpret bytes as text.

Lesson summary

  • Binary uses 0 and 1, with powers of two as place values.
  • Hexadecimal uses 0–9 and A–F; each hex digit represents four bits.
  • Character encodings connect characters with numeric codes or byte patterns.
  • ASCII covers a limited character set. Unicode assigns code points across many writing systems, and UTF-8 encodes Unicode text as bytes.
  • A character may use more than one byte, and byte patterns need an encoding to be interpreted as text.

Check your understanding

Question 1

What is the decimal value of binary 10010?
  1. 18
  2. 10
  3. 20
  4. 10010
Show answer and explanation
18
The 1 bits mark the 16 and 2 places. Their total is 18.

Question 2

Which four-bit pattern represents hexadecimal B?
  1. 1011
  2. 1100
  3. 1001
  4. 1110
Show answer and explanation
1011
B represents eleven. The pattern 1011 marks eight, two, and one, which add to eleven.

Question 3

What does UTF-8 do?
  1. It encodes Unicode text as bytes, using one or more bytes for a character.
  2. It makes every character exactly one byte long.
  3. It converts every number to hexadecimal.
  4. It is another name for binary place values.
Show answer and explanation
It encodes Unicode text as bytes, using one or more bytes for a character.
UTF-8 encodes Unicode text as bytes. Some characters use more than one byte.

Key terms

Bit
A binary digit with value 0 or 1.
Byte
A group of eight bits.
Binary
A base-2 number system using 0 and 1.
Hexadecimal
A base-16 number system using 0–9 and A–F.
Character encoding
A set of rules connecting characters with numeric codes or byte patterns.
Unicode
A standard that assigns code points to characters across many writing systems.
UTF-8
A way of encoding Unicode text as bytes.

Continue through ICS3U

View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation A1.2. It is a study resource, not an official curriculum publication.

Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.

Official curriculum reference

Report a correction or ask a question