Last active
September 29, 2020 01:40
-
-
Save Anaminus/4d53fd85a010544c44f855e7bf59815e to your computer and use it in GitHub Desktop.
UnwrapBase unwraps a number to a given base for a given number of digits.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- UnwrapBase unwraps a number to a given base for a given number of digits. | |
| -- | |
| -- Examples: | |
| -- 3-digit decimal: UnwrapBase(42, 10, 3) -- 2, 4, 0 | |
| -- 8-bit binary: UnwrapBase(85, 2, 8) -- 1, 0, 1, 0, 1, 0, 1, 0 | |
| -- DWORD: UnwrapBase(0xDEADBEEF, 256, 4) -- 249, 190, 173, 222 | |
| -- | |
| local function UnwrapBase(value, base, length) | |
| if not length then | |
| length = math.ceil(math.log(value+1)/math.log(base)) | |
| end | |
| local output = {} | |
| for i = 0, (length or 1) - 1 do | |
| output[i+1] = math.modf(value / base^i % base) | |
| end | |
| return unpack(output) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment