ScratchData LogoScratchData
Back to DerikK's profile

Base Converter

DEDerikK•Created February 18, 2013
Base Converter
7
1
546 views
View on Scratch

Instructions

This base converter will convert any positive decimal number to any other base less than base 10. It uses the modular arithmetic function to calculate each digit in the given base. Bases above base 10 are not supported due to the lack of an easy way of adding transdecimal digits in Scratch. Javascipt code for those interested: var originalInput = prompt("What decimal number would you like to convert?"); var input = originalInput; var wasNegative = false; if (originalInput<0) { input = Math.abs(input); wasNegative = true; } var base = 0 while (base < 2 || base > 10) { base = prompt("What sub-decimal base would you like to convert it to?"); if (base < 2 || base > 10) { alert("I can't convert into base " + base + "."); } } var placeValue = 1; var output = 0; while (input) { output += ((input%base)*placeValue); input -= input%base; input = input/base; placeValue = placeValue*10; } if (wasNegative) { output = "-" + output; } alert(originalInput + " in base " + base + " is " + output + ".");

Description

Put in any decimal number and this base converter will convert it to the sub-decimal base of your choice.

Project Details

Project ID10053949
CreatedFebruary 18, 2013
Last ModifiedMay 31, 2013
SharedFebruary 25, 2013
Visibilityvisible
CommentsAllowed