ExamplesBase conversions
Base conversions
Convert a number to hexadecimal and validate binary text.
On this page
These examples extend number bases. They use non-negative integers and text representations, without arrays.
Decimal to hexadecimal
MiniScript+
n = 254x = nalphabet = "0123456789ABCDEF"result = ""IF x == 0 THEN result = "0"ENDWHILE x > 0 result = CHAR(alphabet, x MOD 16) + result x = x DIV 16ENDPRINT resultThe result is FE because 15 * 16 + 14 = 254. Remainders 14, 15 select E, F from the alphabet and are prepended to the result. Zero prints 0.
Binary to decimal with digit validation
MiniScript+
INPUT textvalid = LEN(text) > 0value = 0FOR i FROM 0 TO LEN(text) - 1 character = CHAR(text, i) IF character == "0" OR character == "1" THEN value = value * 2 + ORD(character) - ORD("0") ELSE valid = FALSE ENDENDIF valid THEN PRINT valueELSE PRINT "Invalid representation"ENDEnter "1101" with quotes in the editor console to read it as text: expect 13. Inputs "102" and "" produce Invalid representation. Every new digit shifts the accumulated value one position in base two. Use short text so the result remains within the exact integer range.
Try it
Convert 16, 255, 256: expect 10, FF, 100. For binary, test "0", "0011", and text containing spaces; spaces are invalid under this definition.