简体   繁体   中英

Don't understand Character.digit in Java

I'm not sure I understand what this method does. Can someone explain ? All I know, from what I read, is that "This method returns the numeric value represented by the character in the specified radix." I'm not sure what a radix is and what it represents. According to the description of what is being returned, I assumed this method gets a character and returns the numeric value of it in the ASCII table, but I guesses that's not true ?

Are you familiar with numerical bases?

For example, '3' in base 10 is equal to 3, '101' in base 2 is equal to 5 in base 10, etc.

That's essentially what Character.digit does -- it takes a character, a specified base, and returns the numerical value in base 10.

If you provide it with a value greater then the specified base (for example, Character.digit('3', 2); , it'll just return -1, signifying an invalid value.

The method isDigit is true of the character and the Unicode decimal digit value of the character (or its single-character decomposition) is less than the specified radix. In this case the decimal digit value is returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM