Promo Exam Notes# Data Representation
Data Representation
Note: conversion between octal and another base is NOT in new syllabus
Binary Representation
- Bit = binary digit (1 or 0)
- Computer only understands binary digits ⇒ all data must be represented by patterns of 1’s and 0’s
- Byte: represents 8 bits
- E.g. a 16-bit computer (pic on right)
| Number System | Base | Digits used (front & end inclusive) | Used by |
|---|---|---|---|
| Decimary (Denary) | 10 | 0-9 | Humans |
| Binary | 2 | 0,1 | Computers |
| Octal | 8 | 0-7 | Programmers as ‘shorthands’ for binary |
| Hexadecimal | 16 | 0-9, A-F |
= a number N with base r
- E.g. 6925
binary octal hex decimal
Place value = value of any digit depends on its position in the number
- E.g. decimal number 4957
Place value:
--------------------------
4 9 5
MSD LSD - E.g. binary number
Place value:
--------------------------
1 1 0
MSD LSD - Most Significant Digit (MSD) = digit with highest place value in the number
- Least Significant Digit (LSD) = digit with lowest place value in the number
Conversion of a number from decimal to another base
- Divide decimal number by the new base continuously and note remainders until quotient is 0
- Arrange remainders, with the first remainder as the least significant digit, and last remainder as the most significant digit


Conversion of a number from other base to decimal
- Multiply each digit by its place value and then sum up the products


Conversion of a number from binary to hexadecimal
- Starting from LSD, sector in group of 4. Insert dummy 0’s when there is not enough
- For each group of 4, calculate its decimal value, and it is the digit for hexadecimal


- Note the dummy 0’s are added in front
Conversion of a number from hexadecimal to binary
- Convert each digit into the binary representation of 4 digit
1
Found from web:

Python functions
Dec to oct:
a = oct(15)Dec to bin:
b = bin(15)Bin to dec:
c = int(0b1111)Oct to dec:
d = int(0o17)Bin to oct:
e = oct(0b1111)Oct to bin:
f = bin(0o17)
Tutorial (not marked)
| Question | Answer |
|---|---|
| Binary 1010 | Denary: 10 |
| Denary 27 | Binary: 11011 |
| Denary 63 | Hexadecimal: 3F (or 0x3F2?) |
| Hexadecimal B48 | Denary: 3144 |
| Binary 10110 | Hexadecimal: 16 |
| Hexadecimal FF60 | Binary: 1111111101100000 |
Q: For each conversion below, write a non-recursive and recursive function.
Design your function with the data type of integer for decimal numbers, and string for binary and hexadecimal numbers (not marked but tested on code).
# Q: For each conversion below, write a non-recursive & recursive function.
Design your function with the data type of integer for decimal numbers, and string for binary and hexadecimal numbers
(not marked but tested on code)
# Decimal to binary (non-recursive):
def decimal_to_binary(decimal):
remainders = ''
while decimal != 0:
remainder = decimal % 2
remainders = str(remainder) + remainders
decimal = decimal // 2
return(remainders)
# NOTE: print(remainders.reverse()) will print None
def DecToBin(decimal):
binary = ''
while decimal != 0:
quotient = decimal // 2
remainder = decimal % 2
# add the remainder as a string
binary = str(remainder) + binary
# update decimal for next round of calculation
decimal = quotient
return decimal
# Decimal to binary (recursive):
def recursive_decimal_to_binary(decimal):
if decimal == 0:
return ''
return recursive_decimal_to_binary(decimal // 2) + str(decimal%2)
# print(recursive_decimal_to_binary(10))
# NOTE: order is opp of non-recursive!
def DecToBinRec(decimal):
if decimal // 2 == 0: # when quotient is 0, stop the recursion and return remainder
return str(decimal % 2) # return digit as a string
else: # when quotient is not 0, continue division
quotient = decimal // 2
remainder = decimal % 2
return str(DecToBinRec(quotient)) + str(remainder) # add the digits as strings
# Decimal to hexadecimal (non-recursive):
def decimal_to_hexadecimal(decimal):
hex_digits = '0123456789ABCDEF'
remainders = ''
while decimal != 0:
remainder = decimal % 16
remainders = hex_digits[remainder] + remainders
decimal = decimal // 16
return remainders
def DecToHex(decimal):
hexa = ''
while decimal != 0:
quotient = decimal // 16
remainder = decimal % 16
# add the remainder as a string
chars = '0123456789ABCDEF'
hexa = chars[remainder] + hexa
# update decimal for next round of calculation
decimal = quotient
return hexa
# Decimal to hexadecimal (recursive):
hex_digits = '0123456789ABCDEF'
def recursive_decimal_to_hexadecimal(decimal):
if decimal == 0:
return ''
remainder = decimal % 16
return recursive_decimal_to_hexadecimal(decimal // 16) + hex_digits[remainder]
recursive_decimal_to_hexadecimal(decimal)
def DecToHexRec(decimal):
if decimal // 16 == 0: # when quotient is 0, stop the recursion and return remainder
return str(chars[decimal % 16])
else: # when quotient is not 0, continue division
quotient = decimal // 16
remainder = decimal % 16
chars = '0123456789ABCDEF'
return str(DecToHexRec(quotient)) + chars[remainder] # add the digits as strings
# Binary to decimal (non-recursive):
# Hexadecimal to decimal:
def binary_to_decimal(binary):
res = 0
binary = str(binary)[::-1] # reverse string
for i in range(len(binary)):
digit = int(binary[i])
value = digit * (2i)
res += value
return res
def BinToDec(binary):
decimal = 0
power = len(binary) - 1
for digit in binary:
decimal += int(digit) * (2 power)
power -= 1
return decimal
# Binary to decimal (recursive):
def BinToDecRec(binary):
if binary == '':
return 0
else:
return int(binary[0]) * (2 (len(binary) - 1)) + BinToDecRec(binary[1:])
# Hexadecimal to decimal (non-recursive):
def hex_to_dec(hex):
hex = str(hex)[::-1]
hex_digits = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
res = 0
i = 0
for char in hex:
res += hex_digits.index(char) * (16i)
i += 1
return res
print(hex_to_dec('6D'))
def HexToDec(hexa):
decimal = 0
power = len(hexa) - 1
for char in hexa:
# convert digits of 0-9, A-F into values of 0-15
if char.isdigit():
value = int(char)
else:
value = ord(char) - ord('A') + 10
decimal += value * (16 power)
power -= 1
return decimal
# Hexadecimal to decimal (recursive):
def recursive_hex_to_dec(hex):
hex_digits = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
if hex == '':
return 0 # dont return '' if not it'll be str when 'else' returns int
else:
return int(hex_digits.index(hex[0]) * (16(len(hex) - 1))) + recursive_hex_to_dec(hex[1:])
print(recursive_hex_to_dec('6D'))
def HexToDecRec(hexa):
if hexa == '':
return 0
else:
char = hexa[0]
if char.isdigit():
decimal = int(char)
else:
decimal = ord(char) - ord('A') + 10
return decimal * (16 (len(hexa) - 1)) + HexToDecRec(hexa[1:])
# Binary to hexadecimal (non-recursive):
# wrong: uses other functions
def bin_to_hex(bin):
bin = str(bin)
if len(bin) % 4 != 0:
bin = '0' * (4 - (len(bin) % 4)) + bin # zero padding
res = ""
for i in range(0, len(bin), 4):
block = bin[i:i+4]
dec_digit = str(binary_to_decimal(block))
res += decimal_to_hexadecimal(int(dec_digit))
return res
print(bin_to_hex(10101001010111))
# ryan’s ans
def BintoHex(bin_str):
bin_hex = {
'0000': '0',
'0001': '1',
'0010': '2',
'0011': '3',
'0100': '4',
'0101': '5',
'0110': '6',
'0111': '7',
'1000': '8',
'1001': '9',
'1010': 'A',
'1011': 'B',
'1100': 'C',
'1101': 'D',
'1110': 'E',
'1111': 'F'
}
bin_str = '0'*((4-(len(bin_str) % 4))%4) + bin_str
split_str = []
res = ''
for i in range(0, len(bin_str), 4):
split_str.append(bin_str[i:i+4])
for char in split_str:
res += bin_hex[char]
return res
print(BintoHex('11011010'))
def BinToHex(binary):
# add extra 0s
binary = '0' * (4-len(binary) % 4) + binary
hexa = ''
for i in range(0, len(binary), 4):
# extract blocks of 4 bits
block = binary[i:i+4]
# calculate the decimal value of each block
decimal = 0
power = 3
for digit in block:
decimal += int(digit) * (2 power)
power -= 1
# match the decimal value to hexa
chars = '0123456789ABCDEF'
hexa += chars[decimal]
return hexa
# Binary to hexadecimal (recursive):
# wrong: uses other functions
def recursive_bin_to_hex(bin):
bin = str(bin)
if bin == '': # empty string; base case for recursion
return ''
else:
if len(bin) % 4 != 0:
bin = '0' * (4 - (len(bin) % 4)) + bin # zero padding
block = bin[0:4] # index of end = 3 (4th digit); [start:end+1]
dec_digit = str(binary_to_decimal(block))
res = decimal_to_hexadecimal(int(dec_digit))
return res + recursive_bin_to_hex(bin[4:])
print(recursive_bin_to_hex(10101001010111))
def block(binary):
binary = '0' * (4 - len(binary)) + binary
decimal = 0
power = 3
for digit in binary:
decimal += int(digit) * (2 power)
power -= 1
chars = '0123456789ABCDEF'
return chars[decimal]
def BinToHexRec(binary):
if len(binary) < 5:
return block(binary)
else:
# binary[:-4] - first to before last 4
# bianry[-4:] - last 4 digit
return BinToHexRec(binary[:-4]) + block(binary[-4:])
# Hexadecimal to binary (non-recursive):
def hex_to_bin(hex):
hex_to_bin_dict = {'0':'0000','1':'0001','2':'0010','3':'0011','4':'0100','5':'0101','6':'0110', '7':'0111','8':'1000','9':'1001',
'A':'1010','B':'1011','C':'1100', 'D':'1101', 'E':'1110', 'F':'1111'}
res = ''
for i in range(len(hex)):
bin = hex_to_bin_dict[hex[i]]
res += bin
return res
print(hex_to_bin('DAB'))
# wrong: uses other functions
def hex_to_bin(hex):
res = ''
for digit in hex:
decimal = hex_to_dec(digit)
binary = decimal_to_binary(decimal)
binary = str(binary)
if len(binary) < 4:
binary = '0' * (4-(len(binary))) + binary
res += binary
return res
print(hex_to_bin('2A57'))
# note: zero padding for each hex->bin, not get binary of everything first then zero pad
⇒ each digit in hex should correspond to 4 digits in binary
# Hexadecimal to binary (recursive):
# wrong: uses other functions
def recursive_hex_to_bin(hex):
if hex == '':
return "" # return empty string; cannot just return else will return None that cannot be concatenated to string
else:
decimal = hex_to_dec(hex[0])
binary = decimal_to_binary(decimal)
binary = str(binary)
if len(binary) < 4:
binary = '0' * (4-(len(binary))) + binary
return binary + recursive_hex_to_bin(hex[1:])
print(recursive_hex_to_bin('2A57'))
def HexToBin(hexa):
binary = ''
for char in hexa:
# convert hexa to the decimal value
if char.isdigit():
decimal = int(char)
else:
decimal = ord(char) - ord('A') + 10
# convert decimal value to binary number
binary_block = ''
while decimal != 0:
quotient = decimal // 2
remainder = decimal % 2
# add the remainder as a string
binary_block = str(remainder) + binary_block
# update decimal for next round of calculation
decimal = quotient
# add the 4-digit binary
if len(binary_block) != 4:
binary_block = '0' * (4 - len(binary_block)) + binary_block
binary += binary_block
return binary
def HexToBinRec(hexa):
if hexa == '':
return ''
else:
first = hexa[0]
if first.isdigit():
decimal = int(first)
else:
decimal = ord(first) - ord('A') + 10
# convert decimal value to binary number
binary = ''
while decimal != 0:
quotient = decimal // 2
remainder = decimal % 2
# add the remainder as a string
binary = str(remainder) + binary
# update decimal for next round of calculation
decimal = quotient
# add the four-digit binary
if len(binary) != 4:
binary = '0' * (4 - len(binary)) + binary
return binary + HexToBinRec(hexa[1:])Ans key: DataRepresentationAndCheckDigit.pdf