All possible characters to represent a number as a string, Integer.java

Integer.java has the following code snippet:

/** * 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 that all the numbers / symbols you will ever need are in the range 0-9, and the letters A are F. The letters (A, B, C, D, E and F) will be used only when the numbers will be represented in base 16 (hexadecimal).

Why does Javadok say "All possible characters"? Are letters from G to Z used? I would think that they can be used if the base, we represent numbers in, are greater than 16.

+6
source share
3 answers

The toString method supports arbitrary bases (e.g. 20) up to MAX_RADIX , which are defined as 36

+11
source

β€œBase36: Practical Use” explains some common use cases.

  • The remote visualization protocol for bulletin board systems uses base notation 36 to transmit coordinates in a compact form.
  • Many URL redirect systems, such as 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 to avoid vowels, vowels, and other confusions.
  • Various systems, such as RickDate, use base 36 as a compact representation of Gregorian dates in file names, using one digit every day and month.
  • [and much more]

Protocol developers sometimes require a compact, ASCII alphanumeric, case-insensitive integer encoding scheme. Base36 fits the bill.

+6
source

it supports a base base of up to 36 when converting an integer to a string representation.

Both MAX and MIN radix are specified in the character class as

 public static final int MIN_RADIX = 2; public static final int MAX_RADIX = 36; 

try something like this:

 for (int i = 2; i < 36; i++) { System.out.println("Radix = "+i+" -- " + Integer.toString(123, i)); } 

Output:

 Radix = 2 -- 1111011 Radix = 3 -- 11120 Radix = 4 -- 1323 Radix = 5 -- 443 Radix = 6 -- 323 Radix = 7 -- 234 Radix = 8 -- 173 Radix = 9 -- 146 Radix = 10 -- 123 Radix = 11 -- 102 Radix = 12 -- a3 Radix = 13 -- 96 Radix = 14 -- 8b Radix = 15 -- 83 Radix = 16 -- 7b Radix = 17 -- 74 Radix = 18 -- 6f Radix = 19 -- 69 Radix = 20 -- 63 Radix = 21 -- 5i Radix = 22 -- 5d Radix = 23 -- 58 Radix = 24 -- 53 Radix = 25 -- 4n Radix = 26 -- 4j Radix = 27 -- 4f Radix = 28 -- 4b Radix = 29 -- 47 Radix = 30 -- 43 Radix = 31 -- 3u Radix = 32 -- 3r Radix = 33 -- 3o Radix = 34 -- 3l Radix = 35 -- 3i Radix = 36 -- 3f 
+2
source

Source: https://habr.com/ru/post/986660/


All Articles