Could someone please help me understand the significance of radix in the Character.forDigit(int digit, int radix)
method?

- 714,442
- 84
- 1,110
- 1,523

- 89
- 1
- 10
-
Radix is the base in which you count. We count in base 10 normally, but not always - especially not in the computer world. – RaminS Mar 26 '17 at 22:47
-
Java isn't an acronym; don't use all-caps. – Nic Mar 26 '17 at 22:49
-
I've reopened this because the answer for `Character.forDigit` is not the same as the answer for `Integer.toString` or `Integer.parseInt`, and the answer given as a duplicate did not cover `forDigit`. – ajb Mar 26 '17 at 22:56
3 Answers
Choosing a suitable radix lets you produce "digits" that are not decimal digits - for example, if you pass 16 for the radix, you can produce characters for digits ten through fifteen, because counting in hex uses sixteen digits - 0 through 9, followed by A through F:
char ten = Character.forDigit(10, 16); // returns 'a'

- 714,442
- 84
- 1,110
- 1,523
This is tricky because the significance isn't as obvious as it first appears. When converting a string to an integer, of course the radix matters a lot. If you are converting "101"
to an integer, you will get different answers depending on whether the radix (base) is binary (2), decimal (10), octal (8), hex (16), or any other base. Similarly, when converting an integer to a string, the results (when the source is >= MAX_RADIX) are all different for the different radices.
For forDigit
, the answer isn't as clear. When you're converting a number to a single character representing a digit, the answer is always the same as long as the digit is valid for the radix. Thus, Character.forDigit(11,radix)
always returns 'b'
for all radices 12 and up. So the only significance is in how it handles the case when the digit is not valid for the radix? That is, for binary (radix=2), forDigit
only works if the digit is 0 or 1; so what should it do if you say Character.forDigit(2,2)
, since 2 is not a valid binary digit?
There are a few things the language designers could have done: (1) get rid of the radix parameter and put the onus on the programmer to make sure the digit is in range (which in many cases will be a given anyway); (2) throw an exception; (3) return some special value. They chose (3): if you give it a digit that isn't valid for the radix, it returns '\0'
, the null character. This doesn't seem to be the best choice--you're unlikely to really want to use the null character for anything, which means you have to make your own check, which means they probably should have had the method throw an exception. But there it is.
But anyway, that's the significance of radix
for this method: it performs a check to make sure the argument is in range, based on the radix.

- 31,309
- 3
- 58
- 84
It is the base of the number. One normally uses base 10 (ie 0-9). However, you might also be interested in using hexadecimal (ie 0-9, A-F), for example. The radix would then be 16.
Example:
Character.forDigit(8, 16)=8
Character.forDigit(9, 16)=9
Character.forDigit(10, 16)=a
Character.forDigit(11, 16)=b

- 3,220
- 1
- 18
- 39