Skip to content

Instantly share code, notes, and snippets.

@raf202
Created August 30, 2022 00:18
Show Gist options
  • Save raf202/0ad328e05e527c352510dd413850a41f to your computer and use it in GitHub Desktop.
Save raf202/0ad328e05e527c352510dd413850a41f to your computer and use it in GitHub Desktop.
Solidity Convert to base
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