Binary to decimal conversion

This guide has been design to help you create a simple binary to decimal conversion program. Follow these step-by-step instructions to create a useful conversion program.

Use "Ask" and "Set" to get user input to a calculation.

Stage 1 - Making the background

Start a new Scratch project - "File" and then "New"

Switch to the stage and click on "backgrounds".

Use the line drawing tool to draw this grid as closely as possible to the example.

Now use the text tool to write the titles for the grid. You will have to use spaces to separate the words.

The little blach dot to the left and above "Eights" is the handle that you can use to move the titles to the right place. Like this... then press "OK"

Stage 2 - Communicating with the user

Now we need the get the cat to tell the user what is going to happen like this...

When you run this "thread" the cat will say "You are going to type in a 4 bit binary number." for two seconds which may not be long enough so change it to be 5 seconds.

An "ask" block will ask a question and wait for a response. So ask the question "Please type in the first binary number. It must be 0 or 1." ...

... like this:

The next step is to store the answer in a variable. I shall call the variable "eights". Click on "variables" then "Make a variable" and call the variable "eights".

From the varables well select "set variable to" and drag it in and then choose"answer" from "sensing".

So, "ask" asks a question and "set variable to" stores the answer, We can do this with the next 3 variables, "fours", then "twos" and "ones"

Note that I have arranged the variables on the screen so that they are in the right place on the grid.

Stage 3 - Making the calculation

Now for the arithmetic. We need another variable which can be called "total" ...

, if any of the ones, twos, fours, or eights variables is 1 then we add 1, 2, 4 or 8 to the toal like this...

Just to add some suspense we will pretend that the computer is thinking by adding a dramatic pause to the calculation, like this...

Now the computer can give the answer...

However, can you spot the mistake. I have rune the program twice and the total variable has not forgotten the first answer...

... there needs to be a way of setting the variables back to zero when the prgram starts.

Stage 4 - Initialistion

Add this new thread.

Have you noticed that I have moved the cat so that there is room to read the comments and see the variables.

Your finished project should look something like this...

 

Is the decimal value of 1101 really 13?

Stage 5 - Extension

See if you can extend the calculation so that an 8 bit number can be converted.

Now make scratch convert a decimal number to binary.

This makes use of "lists" a variable that contains an unknown number of similar items.

Begin as always with "File" and then "New" to begin a new project.

This is the algorithm (sequence of instruction to solve a problem). IN words the decimal number is continually divided by 2 until there is nothing left. The remainder is put to one side. The answer is the remainders read from bottom to top.

The trick here is that we do not know how many variables we would need. This is the reason for the list.

The first thing to do is to get a few variables and lists. We need variables "decimal" to store the decimal number we are converting, "remainder" to store the remainder of each calculation and lists "binary" to store the results of all the calculations and "answer" which is the"binary" list written backwards.

Now begin the script that introduces the program with a "say" block and then ask the user for the number that they want to convert as an "ask" block; like this...

Next pass the value that the user typed in to the variable "decimal" using a "set variable to" block. When you run the program now the variable "decimal" should contain the number that you typed in .

Now add a code blaock that tells the user what is being done, Say it for 2 seconds.

Next we are goiong to work on the algorithm using an "If ... else" block.

There is a form of arithmetic that works on whole numbers only; "Mod". Mod only shows the remainder, so for example 17 mod 2 is 1 and 8 mod 2 is 0.

We can use this to help with the arithmetic. We need to know if the remainder is 0 or 1 so add an "operator" blcok to the "If ... else" block so that it says "if decimal mod 2 = 1" like this ...

We need to repeat this process until decimal is zero (look at the algorithm again to see why) so put the "If ... else" block in a repeat loop with "decimal = 0" as the condition.

There are two possible results, "decimal mod 2" can have the result of 0 or 1.

If the result is 1 then set the remainder to be 1 and decimal to be (decimal - 1) / 2
This gets the decimal value ready for the next iteration (technical word meaning the next go round the algorithm).

Now the other possibility, the answer to "decimal mod 2" is 0. In this case set the remainder to be 0 and decimal to be decimal / 2


Next we need to add "remander" to the list. There is a block on the varaibles well that says "Add thing to list", use this block and make it say "add remainder to binary" in each of the "if" and "else" blocks.

Now comes the list bit that may be a littel difficult.
We need to mke sure that the "answer" list is empty so add a "repeat" block where the condition is "length of answer = 0" and then in the loop add a block that says "delete 1st of answer". This will go through the list as many times as it takes deleting the first entry in the list each time until there are no more entries in the list.

Next add another repeat block that will add the last of binary to answer and then delete the last of binary. If this is done until ther is nothing left in binary then answer will have the same values as binary but in reverse order.

Finally add a "say" block that shows the answer

The programs works well and should cope with any sizr of number.

 

So what is the binary for 106?

 

Recent Posts

July 24

Repairing a hyperspace window

Phasellus quam turpis, feugiat sit amet ornare in, hendrerit in lectus semper mod quisturpis nisi consequat etiam lorem. Phasellus quam turpis, feugiat et sit amet ornare in, hendrerit in lectus semper mod quis eget mi dolore.

July 18

Adventuring with a knee injury

Phasellus quam turpis, feugiat sit amet ornare in, hendrerit in lectus semper mod quisturpis nisi consequat etiam lorem. Phasellus quam turpis, feugiat et sit amet ornare in, hendrerit in lectus semper mod quis eget mi dolore.

July 14

Preparing for Y2K38

Phasellus quam turpis, feugiat sit amet ornare in, hendrerit in lectus semper mod quisturpis nisi consequat etiam lorem. Phasellus quam turpis, feugiat et sit amet ornare in, hendrerit in lectus semper mod quis eget mi dolore.

Spotlight

Neural Implants

The pros and cons. Mostly cons.

Phasellus quam turpis, feugiat sit amet ornare in, hendrerit in lectus semper mod quisturpis nisi consequat ornare in, hendrerit in lectus semper mod quis eget mi quat etiam lorem. Phasellus quam turpis, feugiat sed et lorem ipsum dolor consequat dolor feugiat sed et tempus consequat etiam.

Lorem ipsum dolor quam turpis, feugiat sit amet ornare in, hendrerit in lectus semper mod quisturpis nisi consequat etiam lorem sed amet quam turpis.