Binary Converter

Introduction

Alright, it's not a game. This is the basic code required to convert and display numbers in binary form. There are a number of ways, described at the end, that you could extend this program to make it more useful to you.

MakeCode Arcade Code

Adding The Extension

This program makes use of the Seven Segment extension to show the decimal value of the number. You need to add this extension to get the program working.

MakeCode Arcade Code

MakeCode Arcade Code

Setting Up The Place Values

This function defines the images to use for 0 and 1 and makes 8 sprites lined up nicely at the top of the screen. It also makes an array from the list of sprites. If you are going to add any other sprites to the program, you will need to make sure that they are different kinds from this one.

MakeCode Arcade Code

Making The Binary

This function does the conversion. It uses a simple, classic algorithm to do this, using remainders from division by 2. It sets the values of the sprites. It presumes that it receives a denary value that is in the range 0 - 255. The rest of this example program ensures that this is the case. Click on the image to view a larger version.

MakeCode Arcade Code

Quick Test

At the start of the program, we need to set up a counter from the 7 segment library and call our two functions. Click the image for a larger version.

MakeCode Arcade Code

Changing The Value

MakeCode Arcade Code

MakeCode Arcade Code

Notice that we only allow a change to the counter value if it will remain in our valid range.

Extensions & Challenges

  1. You could add the ability to enter a denary or binary value directly using the dialogs from the Game section. You will need to validate the input to ensure that your value does not end up falling out of the range of valid numbers.
  2. You could add the ability to enter and display a binary value. You could use dialogs for this or work out a more complex set of interactions with the sprites.
  3. You could use the binary conversion for some more interesting purpose - perhaps a quiz or some other way of making something more game-like.