Created
August 30, 2022 00:18
-
-
Save raf202/0ad328e05e527c352510dd413850a41f to your computer and use it in GitHub Desktop.
Solidity Convert to base
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
function convertToBase(uint N, uint8 base) public returns (uint) { | |
if (N <= 0) return 0; | |
uint index = 0; | |
uint converted = 0; | |
while (N >= base) { | |
uint remainder = N % base; | |
N = N / base; | |
converted += remainder * (10** index++); | |
if(N < base){ | |
converted += N * (10** index++); | |
} | |
} | |
return converted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment