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.
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":
And then I created my own code block (function) that uses the variables and does the conversion:
Using this new code block, I'm able to convert from decimal to binary: