简体   繁体   English

方法幕后发生了什么-公共静态长parseLong(String s,int radix)

[英]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.

相关问题 为什么Java使用这个长二进制数来使用Long.parseLong(String s,int radix)会冒犯? - Why is Java offended by my use of Long.parseLong(String s, int radix) with this long binary number? Long 类型中的 parseLong(String) 方法不适用于参数 (char) - The method parseLong(String) in the type Long is not applicable for the arguments (char) NumberFormatExcpetion 同时使用 Long.parseLong() 将 base-2 数字(作为字符串)解析为 base-10 Long int - NumberFormatExcpetion while using Long.parseLong() to parse a base-2 number(as a string) to a base-10 Long int Long.parseLong(String s)和new Long(String s)之间的区别? - Difference between Long.parseLong(String s) and new Long(String s)? Jave-方法是-parseByte(String s,int radix)-我听不懂 - Jave - method is - parseByte(String s, int radix) - I can't understand it 将长字符串转换为整数而不使用parseLong或parseInt - Convert long string to integer without parseLong or parseInt 为什么Long.parseLong(String s)不会将尾随的“L”视为有效? - Why Long.parseLong(String s) won't consider a trailing “L” as valid? 目标C中的Integer.parseInt(String s,int radix)等效 - Integer.parseInt(String s, int radix) equivalent in Objective C Java的BufferedInputStream的幕后花絮 - Behind the scenes of Java's BufferedInputStream Long.parseLong的NumberFormatException - NumberFormatException for Long.parseLong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM