关于GUID的问题:它们的长度是否总是固定的,中间的数字是否总是4? [英] Questions about GUID's: Are they always fixed in length, and is the middle number always 4?

查看:288
本文介绍了关于GUID的问题:它们的长度是否总是固定的,中间的数字是否总是4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚生成了几百万个GUID,将它们转换为String并得到了长度...它总是一样的.转换为String时,可以依靠GUID的此固定长度吗?

I just generated a few million GUID's turned them into a String and got the length... it was always the same. Can I rely on this fixed length of the GUID when converting to String?

此外,如该屏幕截图所示,GUID的中间数字是否始终为"4"?

Also, is the middle number of the GUID always "4" as shown in this screenshot?

推荐答案

是的,长度是固定的,是的,当您使用标准tostring格式时,中间数字始终为4. GUID中的某些位(几乎在任何非Windows的地方都称为UUID)已固定,以指示版本等信息.

Yes, the length is fixed and yes, the middle number is always 4 when you use the standard tostring format. Some of the bits in GUID (known as a UUID almost anywhere that isn't windows) are fixed to indicate things like version etc..

http://en.wikipedia.org/wiki/Uuid

编辑 我应该补充一点,"4"仅适用于根据.NET中实现的Guid.NewGuid算法生成的Guid.没有什么可以阻止您获取任意字节[16]并将其转换为Guid的.因此,对于.Net中算法的当前实现,您只能依靠它为4.如果您从其他来源获得Guid,则不能依靠4.对.Net或Windows(取决于.Net使用自己的还是Windows的生成器)进行更新可能会更改GUID的固定数量

EDIT I should add that the "4" only applies to Guids that have been generated according to the Guid.NewGuid algorithm as implemented in .NET. There is nothing to stop you from taking any arbitrary byte[16] and converting it to Guid. So, you can only bank on it being 4 for the current implementation of the algorithm in .Net. If you are getting Guids from another source, you can't bank on the 4. An update to .Net or possibly windows(depending if .Net uses its own or Windows' generator) may change the fixed numbers of the GUID

例如以下是完全有效的代码,不会在第4个位置放置

e.g. the following is completely working code and will not have the 4 in position:

        var rand = new Random();
        var byteArray = new byte[16];
        rand.NextBytes(byteArray);
        var g = new Guid(byteArray);

这篇关于关于GUID的问题:它们的长度是否总是固定的,中间的数字是否总是4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆