Solution: Number Base Conversion
Look at different ways to implement solutions to these number conversion problems.
Decimal to hex
Press + to interact
hex_digit = "0123456789abcdef".split("")hexadecimal = ""while(number != 0)hexadecimal = hex_digit[number % 16].to_s + hexadecimalnumber = number / 16end
Explanation
Line 1: We create an array of characters representing hex values against the decimal numbers 0–15.
Line 4: The remainder—from the division of
number
by 16—is first converted to a hexadecimal string and prepended to the stringhexadecimal
.Line 5:
number
is ...