Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All possible chars for representing a number as a String, Integer.java

Tags:

java

integer

hex

In Integer.java, there is the following piece of code:

/**
 * All possible chars for representing a number as a String
 */
final static char[] digits = {
    '0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};

I thought all the digits/characters you would ever need are in the range 0-9 and letters A to F. Letters (A,B,C,D,E and F) would be used only when the numbers are represented in base 16 (hexadecimal).

Why does the Javadoc say "All possible chars" ? Are the letters from G to Z actually used ? I would think that they could be used if the base, we represent the numbers in, is greater than 16.

like image 989
Evdzhan Mustafa Avatar asked May 06 '15 13:05

Evdzhan Mustafa


2 Answers

The toString method supports arbitrary bases (like 20) up to MAX_RADIX which is defined as 36

like image 162
adjan Avatar answered Sep 19 '22 10:09

adjan


"Base36: Uses in practice" explains some common use cases.

  • The Remote Imaging Protocol for bulletin board systems used base 36 notation for transmitting coordinates in a compact form.
  • Many URL redirection systems like TinyURL or SnipURL/Snipr also use base 36 integers as compact alphanumeric identifiers.
  • Geohash-36, a coordinate encoding algorithm, uses radix 36 but uses a mixture of lowercase and uppercase alphabet characters in order to avoid vowels, vowel-looking numbers, and other character confusion.
  • Various systems such as RickDate use base 36 as a compact representation of Gregorian dates in file names, using one digit each for the day and the month.
  • [and many more]

Protocol designers sometimes need a compact, ASCII alphanumeric, case-insensitive scheme for encoding integers. Base36 fits the bill.

like image 44
Mike Samuel Avatar answered Sep 18 '22 10:09

Mike Samuel