Sunday, March 2, 2014

Re: Best alternative for Character.getNumericValue(ch)?


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.

[1] http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.1
[2] http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment