Skip to content

Instantly share code, notes, and snippets.

@thechriskent
Created April 28, 2022 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thechriskent/5e3e18126c78f65a7439abea0fb36872 to your computer and use it in GitHub Desktop.
Save thechriskent/5e3e18126c78f65a7439abea0fb36872 to your computer and use it in GitHub Desktop.
If(CountRows(binaryDigits)=0,
ClearCollect(binaryDigits,[]);
ForAll(Sequence(31,30,-1),
Collect(binaryDigits, {
index: Value,
worth: Power(2,Value)
})
)
);
ClearCollect(intToBinary,[]);
With({
firstDigit: CountRows(binaryDigits) - 1, //starting point depending on the number of binary digits
input: Value(txtToBinary.Text)
},
ForAll(binaryDigits,
With({
remainder: If(index = firstDigit, input, Last(intToBinary).remaining) //what's left to process (starts with input and decreases)
},
Collect(intToBinary,{
binary: If(remainder >= worth, "1", "0"), //Actual binary digit (left to right)
remaining: If(remainder >= worth, remainder - worth, remainder) //process what's left
})
)
)
);
Set(binaryResult,
With({
full: Concat(intToBinary,binary)
},
Right(full, Len(full) - Find("1",full) + 1) //trims leading zeros (unnecessary really)
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment