开发者

Whats going on behind the scenes with method - public static long parseLong(String s,int radix)

I need to generate a consistent unique Long based on the name of the开发者_JAVA百科 package. Instead of using "Convert string to long" in Eclipse I think I can achieve the same task at run time by using method public static long parseLong(String s,int radix) ?

I think I need to use something like -

Long.parseLong("Hazelnut", 36) returns 1356099454469L

Which I got from question - How to convert String to long in Java?

Why do I need to set radix to 36 when converting a String that contains characters ?


Well, you're basically to treat it as a number in base 36. So for example, the string "012" would mean 2 + 1 * 36 + 0 * 362. When you run out of digits, you go to letters - so "ABC" would mean 12from 'C' + 11from 'B' * 36 + 10from 'A' * 362.

If you understand how hex works, it's the same except using all the characters in the Latin alphabet.

It'll fail for anything not in 0-9, A-Z, a-z though - and it'll also fail for reasonably long strings; long only works up to 263 which you'll get past reasonably quickly in base 36. "Hazelnut12345" fails, for example. Oh, and this is case-insensitive, so the value for "foo" is the same as for "FOO" - does that fail your uniqueness requirement?

Fundamentally you've only got 264 long values to play with, so unless your package names are pretty restricted you're not going to work out a unique mapping.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜