How to convert base-10 (decimal) to base-2 (binary) in Scratch?

219 Views Asked by At

I have the following function written in the Delphi Programming Language that converts a base-10 (decimal) number to a base-2 (binary) number:

function DecimalToBinary(Value: Int64): string;
const
  tt: array [0 .. 1] of char = ('0', '1');
begin
  Result := '';
  var i: Int64 := Value;
  if i = 0 then Result := '0'
  else
    while (i <> 0) do
    begin
      Result := tt[(i and $1)] + Result;
      i := (i shr 1);
    end;
end;

How can I replicate this function in Scratch?

I want to be able to convert a base-10 (decimal) number to a base-2 (binary) in Scratch.

1

There are 1 best solutions below

2
Shaun Roselt On

Okay. I found a couple of sample projects as well as a tutorial on how to convert decimal to binary in Scratch.

Thanks to the above two links, I was able to create a function in Scratch that converts decimal numbers to binary numbers.

First I created two variables named "binary" and "i":

Scratch variables

And then I created my own code block (function) that uses the variables and does the conversion:

Scratch Decimal to Binary Code Block

Using this new code block, I'm able to convert from decimal to binary:

Scratch Decimal to Binary Code Block