Data representation · GCSE · OCR J277 1.2.4, AQA 8525 3.3.2, Edexcel 1CP2 2.1.6 · about 15 min
Base 16, converting to binary and denary, and the LED's colour codes.
[1 mark]Convert 10110110 to hexadecimal.
[1 mark]What is hex 3F in denary?
[1 mark]Convert 255 to hexadecimal.
[1 mark]Why do programmers use hexadecimal?
[1 mark]What colour is #00FF00 on the LED?
[1 mark]How many bits does one hex digit stand for?
Write to_hex(n) yourself (no hex or format) that turns 0 to 255 into two hex digits. Use it to build the colour code for red 255, green 128, blue 64, print it as #FF8040, and show it on the LED by passing your code to led.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
DIGITS = "0123456789ABCDEF"
def to_hex(n):
return "00"The hint students can ask for: Each byte becomes two hex digits: the first is how many sixteens, the second is what is left over. Look each one up in the digit string, join the six digits behind a hash, print it and give it to the LED.
from bugbot import *
connect()
DIGITS = "0123456789ABCDEF"
def to_hex(n):
return DIGITS[n // 16] + DIGITS[n % 16]
code = "#" + to_hex(255) + to_hex(128) + to_hex(64)
print(code)
led(code)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.