Best alternative for Character.getNumericValue(ch)?

424 views
Skip to first unread message

Ed

unread,
Feb 27, 2014, 5:35:31 AM2/27/14
to google-we...@googlegroups.com
I need to use the method Character.getNumericValue(character) for Iban account nr validation (the modulo calculation part: LINKE)

However this method isn't supported by GWT. What is my (best) alterantive?

Details:
The method for modulo calculation can also be find in the Apache commons: LINKE, method calculateModulus()

Thomas Broyer

unread,
Feb 28, 2014, 4:36:16 PM2/28/14
to google-we...@googlegroups.com
Integer.parseInt("" + ch, 36) should do it, otherwise, simply "('0' <= ch && ch<= '9') ? ch - '0' : ch - 'A' + 10"

Ed Bras

unread,
Mar 2, 2014, 6:00:25 PM3/2/14
to google-we...@googlegroups.com

Thanks thomas, I will have a look at it. I am not sure which situations this covers, and where you get this from, please give me a hint, I am not so well known with this character encoding.

In the mean time, I super-sourced the Character class and implemented the isWhiteSpace() and getNumericValue() method for latin support only (no noticable code increase), in other cases I throw an exception, and will probably never occur in my case. I like this solution as I have shared classes that use these methods also in none-gwt code.

Thomas Broyer

unread,
Mar 2, 2014, 6:26:02 PM3/2/14
to google-we...@googlegroups.com

On Monday, March 3, 2014 12:00:25 AM UTC+1, Ed wrote:

Thanks thomas, I will have a look at it. I am not sure which situations this covers, and where you get this from, please give me a hint, I am not so well known with this character encoding.

In the mean time, I super-sourced the Character class and implemented the isWhiteSpace() and getNumericValue() method for latin support only (no noticable code increase), in other cases I throw an exception, and will probably never occur in my case. I like this solution as I have shared classes that use these methods also in none-gwt code.

For "latin only", and assuming you already validated the input to only contain alphanum (should be the case for IBAN), I'd use Integer.parseInt in base 36 (or the char-based form I gave with the ternary operator, only for uppercase though).
From the Character#getNumericValue javadoc: “The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A') […] forms have numeric values from 10 through 35”, which is the definition of base 36.
In Java, chars are numeric values [1] and can be converted to ints where their value is their codepoint [2], so "ch - 'A'" gives you 0 for 'A', 1 for 'B', etc. and 25 for 'Z', so you just have to add 10 to that to get the base 36 numeric value.

Ed Bras

unread,
Mar 3, 2014, 4:46:41 AM3/3/14
to google-we...@googlegroups.com
@Thomas: thanks for the details, it makes it more clear.
Reply all
Reply to author
Forward
0 new messages