Why does Integer.parseInt(“\uD835\uDFE8”) fail?

2019-06-02 21:48发布

I was under the impression that java supports unicode characters. I made this test and sadly found that it fails. The question is why? Is it a bug or somewhere documented?

// MATHEMATICAL SANS-SERIF "                

1条回答
做个烂人
2楼-- · 2019-06-02 22:22

It's not bug, the behaviour is documented. According to the documentation for parseInt(String s, int radix) (emphasis mine)

The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value

If you try :

int aa = Character.digit('\uD835', 10);
int bb = Character.digit('\uDFE8', 10);

You'll see that both return -1.
Mind you, Integer.parseInt(unicodeNum6); will just call Integer.parseInt(unicodeNum6, 10);

查看更多
登录 后发表回答