[英]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 package. 我需要根据包的名称生成一致的唯一Long。 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) ? 我认为我可以通过使用方法public static long parseLong(String s,int radix)在运行时实现相同的任务,而不是在Eclipse中使用“将字符串转换为long”?
I think I need to use something like - 我想我需要使用类似-
Long.parseLong("Hazelnut", 36) returns 1356099454469L Long.parseLong(“ Hazelnut”,36)返回1356099454469L
Which I got from question - How to convert String to long in Java? 我从问题中得到了什么- 如何在Java中将String转换为long?
Why do I need to set radix to 36 when converting a String that contains characters ? 为什么在转换包含字符的字符串时需要将基数设置为36?
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 * 36 2 . 好吧,您基本上将其视为以36为底的数字。因此,例如,字符串“ 012”表示2 +1 * 36 + 0 * 36 2 。 When you run out of digits, you go to letters - so "ABC" would mean 12 from 'C' + 11 from 'B' * 36 + 10 from 'A' * 36 2 . 当数字用完时,会使用字母-因此,“ ABC”表示“ C”中的 12 + “ B”中的 11 * 36 + “ A”中的 10 * 36 2 。
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, AZ, az though - and it'll also fail for reasonably long strings; 但是,它不会因0-9,AZ和az之外的任何值而失败-并且对于相当长的字符串也会失败; long
only works up to 2 63 which you'll get past reasonably quickly in base 36. "Hazelnut12345" fails, for example. long
最多只能使用2 63 ,您会在base 36中很快通过。例如,“ Hazelnut12345”失败。 Oh, and this is case-insensitive, so the value for "foo" is the same as for "FOO" - does that fail your uniqueness requirement? 哦,这是不区分大小写的,因此“ foo”的值与“ FOO”的值相同-这是否满足您的唯一性要求?
Fundamentally you've only got 2 64 long
values to play with, so unless your package names are pretty restricted you're not going to work out a unique mapping. 从根本上讲,您只能使用2 64个 long
值,因此,除非您的软件包名称受到严格限制,否则您将无法得出唯一的映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.