If the number is already converted to an integer (i.e. base 10)
// Supports up to base 36 char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char FindPlace(int number, int base, int digit) { if(digit < 0) return 0; // Essentially divide the number by [base] to the [digit] power for(i=0; i<digit; i++) { number /= base; } // TODO: Verify that the digit is in range of digits return digits[number % base]; }
( 0 gives you the largest digit, 1 gives you the next digit on the right, etc.)
I returned the digit as char to use databases over 10.
Please note that if you want to allow the user to enter the desired number as โ 1 place, 10 place, 100 placeโ or โ 1 s, 16 s, 256 sโ, you just do
digit = log(PlaceValue, base);
or rewrite the code
char FindPlace(int number, int base, int digitAsBaseToAPower) {
source share