Denary to Binary Conversion Algorithm

In this challenge, we are going to write al algorithm to convert a decimal (aka Denary) number between 0 and 255 into binary using 1 Byte (=8 bits).

Before attempting this challenge you may practise your denary to binary conversion:

Python Challenge


We are now going to use some Python code to implement al algorithm to convert a denary number into binary. To do so, you will need to complete the following 4 tasks.

Python Challenge - Task #1Task #2: Input ValidationTask #3: Writing a FunctionTask #4: Binary to Denary

Python Challenge: Task #1

We have started an algorithm which we would like you to complete to perform a full denary to binary conversion.

Your first challenge is to investigate the code below to see what it does and how it works. You will then need to complete this code to make sure that it outputs a fully Byte of data for any given number between 0 and 255.

The following trace table may help you investigate this code step by step:

 Line NumberdenarybinaryOUTPUT
3

Python Challenge: Task #2

Add some code to your code from task 1 to validate the input so that your code only accepts a number between 0 and 255. You can find out more about different validation techniques below and select the validation technique that is most relevant to this task:

Validation Techniques:


Input Validation: Presence Check

Input Validation: Type Check – Integer?

Input Validation: Range Check

Input Validation: Lookup Check

Input Validation: Character Check

Input Validation: Length Check

Try Again! Using a While Loop:

Task #3: Writing a Function

Turn your code into a function called convertToBinary(). Your function will take a denary number as a parameter and return a binary string. You will then need to add some code to test your function.​

Task #4: Writing a Function

Create a new function called convertToDenary(). This new function will take a binary string as a parameter and return the matching denary value.


unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.7 / 5. Vote count: 14

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!