java.util.UUID.randomUUID().toString() 长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45388021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.util.UUID.randomUUID().toString() length
提问by Yaroslav
Does java.util.UUID.randomUUID().toString() length always equal to 36?
java.util.UUID.randomUUID().toString() 长度是否总是等于 36?
I was not able to find info on that. Hereit is said only the following:
我无法找到相关信息。这里只说以下几点:
public static UUID randomUUID() Static factory to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator. Returns: A randomly generated UUID
public static UUID randomUUID() 用于检索类型 4(伪随机生成)UUID 的静态工厂。UUID 是使用加密强的伪随机数生成器生成的。返回:一个随机生成的 UUID
And that type 4
tells me nothing. I do not know what type 4 means in the case.
而这type 4
什么也没告诉我。我不知道在这种情况下类型 4 是什么意思。
采纳答案by ruhul
Does java.util.UUID.randomUUID().toString() length always equal to 36?
java.util.UUID.randomUUID().toString() 长度是否总是等于 36?
Yes!!it is.
是的!!这是。
A UUID
actually a 128 bit value (2 long). To represent 128 bit into hex string there will be 128/4=32
char (each char is 4bit long). In string format it also contains 4 (-
) that's why the length is 36.
AUUID
实际上是一个 128 位的值(2 长)。要将 128 位表示为十六进制字符串,将有128/4=32
字符(每个字符长 4 位)。在字符串格式中,它还包含 4 ( -
) 这就是长度为 36 的原因。
example: 54947df8-0e9e-4471-a2f9-9af509fb5889
例子: 54947df8-0e9e-4471-a2f9-9af509fb5889
32 hex char + 4 hyphen char = 36 char. So the length will be always same.
32 个十六进制字符 + 4 个连字符 = 36 个字符。所以长度将始终相同。
Update:
更新:
I do not know what type 4 means in the case.?
我不知道在这种情况下类型 4 是什么意思。?
FYI: There are several ways of generating UUID. Here type 4 means this uuid is generated using a random or pseudo-random number. From wiki - Universally_unique_identifier#Versions:
仅供参考:有多种生成 UUID 的方法。这里的类型 4 表示这个 uuid 是使用随机或伪随机数生成的。来自维基 - Universally_unique_identifier#Versions:
Versions
For both variants 1 and 2, five "versions" are defined in the standards, and each version may be more appropriate than the others in specific use cases. Version is indicated by the M in the string representation.
Version 1UUIDs are generated from a time and a node id (usually the MAC address);
version 2UUIDs are generated from an identifier (usually a group or user id), time, and a node id;
versions 3 and 5produce deterministic UUIDs generated by hashing a namespace identifier and name;
and version 4UUIDs are generated using a random or pseudo-random number.
版本
对于变体 1 和 2,标准中定义了五个“版本”,每个版本在特定用例中可能比其他版本更合适。版本由字符串表示中的 M 表示。
版本 1UUID 由时间和节点 ID(通常是 MAC 地址)生成;
版本 2UUID 是根据标识符(通常是组或用户 ID)、时间和节点 ID 生成的;
版本 3 和 5生成通过散列命名空间标识符和名称生成的确定性 UUID;
和版本 4UUID 是使用随机或伪随机数生成的。
回答by Daniel Almeida
You may convert UUIDv4 16 bytes binary to 24 bytes ascii using base64, instead encode to ascii-hex (32 bytes)
您可以使用 base64 将 UUIDv4 16 字节二进制转换为 24 字节 ascii,而不是编码为 ascii-hex(32 字节)
回答by user43968
For those like me that start googling before reading the javadoc, here the javadoc ;)
对于像我这样在阅读 javadoc 之前开始使用谷歌搜索的人,这里是 javadoc ;)
For those that Don't know how to read a grammar tree read from Bottom to Top.
an hexDigitis one char
an hexOctetis 2 hexDigits = 2chars
a nodeis 6 * hexOctet = 6 * 2hexdigit = 6*2 chars = 12chars
a variant_and_sequence is 2 * hexOctet = 2 * 2hexdigit = 2*2 chars = 4chars
a time_high_and_versionis 2 * hexOctet = 2 * 2hexdigit = 2*2 chars = 4chars
a time_midis 2 * hexOctet = 2 * 2hexdigit = 2*2 chars = 4chars
a time_lowis 4 * hexOctet = 4* 2hexdigit = 4*2 chars = 8chars
and finaly, a UUIDis < time_low > "-" < time_mid > "-" < time_high_and_version > "-" < variant_and_sequence > "-"< node >
对于那些不知道如何阅读从底部到顶部阅读的语法树的人。
一个hexDigit是一个字符
一个hexOctet是2个hexDigits = 2chars
一个节点是6 * hexOctet = 6 * 2hexdigit = 6个* 2个字符= 12chars
一个变体_and_sequence是2 * hexOctet = 2 * 2hexdigit = 2个* 2个字符= 4chars
一个time_high_and_version是2 * hexOctet = 2 * 2hexdigit = 2*2 chars = 4chars
a time_mid为 2 * hexOctet = 2 * 2hexdigit = 2*2 chars = 4chars
a time_low为 4 * hexOctet = 4* 2hexdigit = 4*2 chars = 8chars
和finaly , 一个UUID是<time_low>“-”<time_mid>“-”<time_high_and_version>“-”<variant_and_sequence>“-”<节点>
= 8 chars + 1 char + 4 chars + 1 char + 4 chars + 1 char + 4 chars + 1 char + 12 chars
= 8 个字符 + 1 个字符 + 4 个字符 + 1 个字符 + 4 个字符 + 1 个字符 + 4 个字符 + 1 个字符 + 12 个字符
= 36 chars ! 128 bit of data + 4 hyphen as stated previously
= 36 个字符!128 位数据 + 4 个连字符,如前所述
The UUID string representation is as described by this BNF:
UUID = <time_low> "-" <time_mid> "-"
<time_high_and_version> "-"
<variant_and_sequence> "-"
<node>
time_low = 4*<hexOctet>
time_mid = 2*<hexOctet>
time_high_and_version = 2*<hexOctet>
variant_and_sequence = 2*<hexOctet>
node = 6*<hexOctet>
hexOctet = <hexDigit><hexDigit>
hexDigit =
"0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
| "a" | "b" | "c" | "d" | "e" | "f"
| "A" | "B" | "C" | "D" | "E" | "F"