ScripticXLearn programming visually
Platform
Problems
Community
Leaderboard
Learn
Docs
MiniScript+ basicsVariablesOperators and expressionsInput and outputConditions and decisionsLoopsStringsSums, counts, and extremaDigits of a numberDivisibility and prime numbersNumeric sequences and recurrencesNumber basesFunctions and parametersRecursion
Examples
Basic programsConditionsInput and outputLoopsAlgorithmsWorking with textDigit problemsBase conversionsFunctions and recursionEncode text with a key
What's newHelpContact
Loading…

Examples

MiniScript+

ExamplesMiniScript+
Fundamentals
Basic programsConditionsInput and output
Programming patterns
LoopsAlgorithmsWorking with textDigit problemsBase conversionsFunctions and recursionEncode text with a key
Examples/Base conversions

Base conversions

Convert a number to hexadecimal and validate binary text.

On this page
Decimal to hexadecimalBinary to decimal with digit validationTry it

These examples extend number bases. They use non-negative integers and text representations, without arrays.

Decimal to hexadecimal#

MS+MiniScript+
1n = 2542x = n3alphabet = "0123456789ABCDEF"4result = ""5IF x == 0 THEN6  result = "0"7END8WHILE x > 09  result = CHAR(alphabet, x MOD 16) + result10  x = x DIV 1611END12PRINT result

The 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#

MS+MiniScript+
1INPUT text2valid = LEN(text) > 03value = 04FOR i FROM 0 TO LEN(text) - 15  character = CHAR(text, i)6  IF character == "0" OR character == "1" THEN7    value = value * 2 + ORD(character) - ORD("0")8  ELSE9    valid = FALSE10  END11END12IF valid THEN13  PRINT value14ELSE15  PRINT "Invalid representation"16END

Enter "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.

PreviousDigit problemsNextFunctions and recursion

On this page

Decimal to hexadecimalBinary to decimal with digit validationTry it