GUID带有一个始终相等的数字

5
在这台计算机生成的每个GUID的中间,为什么我们会看到一个4?

enter image description here


4
简单的谷歌搜索会返回以下结果: 数字 M 的四个比特表示 UUID 版本。参考链接为:https://zh.wikipedia.org/wiki/%E5%85%A8%E5%B1%80%E5%94%AF%E4%B8%80%E8%AF%86%E5%88%AB%E7%A0%81。 - L.B
好吧,那是一件特别的事情,我认为它与机器有关。 - Marco Salerno
这就是为什么不应该将GUID用作随机数的原因。好吧,第四版本的GUID(也就是这些)由大部分随机位组成,但其中一些位固定以符合RFC 4122的要求。如果需要128个随机位,只需使用一个合适的随机数生成器即可。 - Martin Liversage
2个回答

7

这是 GUID 算法的版本(在您的情况下为4):

https://zh.wikipedia.org/wiki/全局唯一标识符

In its canonical textual representation, the sixteen octets of a UUID are represented as 32 hexadecimal (base 16) digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens). For example:

123e4567-e89b-12d3-a456-426655440000
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

The four bits of digit M indicate the UUID version, and the one to three most significant bits of digit N indicate the UUID variant. In the example, M is 1 and N is a (10xx), meaning that the UUID is a variant 1, version 1 UUID; that is, a time-based DCE/RFC 4122 UUID.


我会接受这个答案,我可能有一个愚蠢的问题,为什么他们要用char - 而不是统一所有东西? - Marco Salerno
@Marco Salerno:对不起,我不明白你的意思:UUID是一个128位数字,可以用xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx表示为字符串。第三个块 - Mxxx实际上是16位十六进制表示的整数。前4位 - M是版本(因此我们可以将其设置在0..15范围内)。我这里看不到任何字符。 - Dmitry Bychenko
我的意思是,为什么当它被表示为字符串时,它们要用“-”分割块? - Marco Salerno
1
@Marco Salerno:第一版的 GUID 遵循 TimeLow-TimeMid-TimeHiAndVersion-ClockSeq-Node 方案,对于分离块的表示非常方便;现在我们使用不同的方法来生成 GUID,但仍然保持旧的表示。 - Dmitry Bychenko

1
这是由于使用的GUID/UUID版本不同导致的; 一些好的信息可以在这里找到,但基本上它们是使用以下格式的第4版:
 xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接