skip to main content
Quorum
Learn
Hour of Code
Reference
Libraries
Download
Bugs
My Projects
Login
Quorum Login
Login
Cancel
Sign up for an account
"Binary Calculator" by Projects
This is a public project. Anyone can view it.
Binary Calculator
Code Area
1
class Main action Main integer value = cast(integer, input("Enter a decimal number to convert to other bases.")) SetDecimalNumber(value) PrintResult() end private integer decimalNumber = 0 private text binary = "" action GetDecimalNumber() returns integer return decimalNumber end action SetDecimalNumber(integer value) decimalNumber = value end action GetBinaryNumber() returns text return binary end action SetBinaryNumber(text value) binary = value end action ConvertDecimalToBinary(integer value) returns text integer quotient = value / 2 integer remainder = value mod 2 text binaryNumber = cast(text, remainder) repeat while quotient > 0 remainder = quotient mod 2 quotient = quotient / 2 binaryNumber = cast(text, remainder) + binaryNumber end SetBinaryNumber(binaryNumber) return binaryNumber end action ConvertBinaryToOctal(text value) returns text text subString = "" integer startIndex = value:GetSize() integer endIndex = value:GetSize() - 3 repeat while startIndex not= 0 if startIndex < 3 subString = OctalSwitchCase(value:GetSubtext( 0, startIndex )) + subString startIndex = 0 else subString = OctalSwitchCase(value:GetSubtext( endIndex, startIndex )) + subString startIndex = endIndex endIndex = endIndex - 3 end end return subString end action OctalSwitchCase(text bitGroup) returns text text octal = "" if bitGroup = "0" or bitGroup = "00" or bitGroup = "000" octal = "0" elseif bitGroup = "1" or bitGroup = "01" or bitGroup = "001" octal = "1" elseif bitGroup = "10" or bitGroup = "010" octal = "2" elseif bitGroup = "11" or bitGroup = "011" octal = "3" elseif bitGroup = "100" octal = "4" elseif bitGroup = "101" octal = "5" elseif bitGroup = "110" octal = "6" elseif bitGroup = "111" octal = "7" end return octal end action ConvertBinaryToHexadecimal(text value) returns text text subString = "" integer startIndex = value:GetSize() integer endIndex = value:GetSize() - 4 repeat while startIndex not= 0 if startIndex < 4 subString = HexadecimalSwitchCase(value:GetSubtext( 0, startIndex )) + subString startIndex = 0 else subString = HexadecimalSwitchCase(value:GetSubtext( endIndex, startIndex )) + subString startIndex = endIndex endIndex = endIndex - 4 end end return subString end action HexadecimalSwitchCase(text bitGroup) returns text text hexadecimal = "" if bitGroup = "0" or bitGroup = "00" or bitGroup = "000" or bitGroup = "0000" hexadecimal = "0" elseif bitGroup = "1" or bitGroup = "01" or bitGroup = "001" or bitGroup = "0001" hexadecimal = "1" elseif bitGroup = "10" or bitGroup = "010" or bitGroup = "0010" hexadecimal = "2" elseif bitGroup = "11" or bitGroup = "011" or bitGroup = "0011" hexadecimal = "3" elseif bitGroup = "100" or bitGroup = "0100" hexadecimal = "4" elseif bitGroup = "101" or bitGroup = "0101" hexadecimal = "5" elseif bitGroup = "110" or bitGroup = "0110" hexadecimal = "6" elseif bitGroup = "111" or bitGroup = "0111" hexadecimal = "7" elseif bitGroup = "1000" hexadecimal = "8" elseif bitGroup = "1001" hexadecimal = "9" elseif bitGroup = "1010" hexadecimal = "a" elseif bitGroup = "1011" hexadecimal = "b" elseif bitGroup = "1100" hexadecimal = "c" elseif bitGroup = "1101" hexadecimal = "d" elseif bitGroup = "1110" hexadecimal = "e" elseif bitGroup = "1111" hexadecimal = "f" end return hexadecimal end action PrintResult() output "Your number in decimal: " + cast(text, GetDecimalNumber()) output "Your number in binary: " + ConvertDecimalToBinary(decimalNumber) output "Your number in octal: " + ConvertBinaryToOctal(binary) output "Your number in hexadecimal: " + ConvertBinaryToHexadecimal(binary) end end
Build (CTRL+B)
Run (CTRL+R)
Stop Program
Save this Project
Project Name:
Save Project
Cancel
Load a Project
Load Project
Cancel
Save
Load
Embed
Output Area