GUID
很久以前就一直在思考GUID跟UUID到底有什麼不一樣
今天就順手的來看一下
首先來看一下C#原始碼發現
https://referencesource.microsoft.com/#mscorlib/system/guid.cs
if (formatCh == 'D' || formatCh == 'd') {
style = GuidStyles.DigitFormat;
}
else if (formatCh == 'N' || formatCh == 'n') {
style = GuidStyles.NumberFormat;
}
else if (formatCh == 'B' || formatCh == 'b') {
style = GuidStyles.BraceFormat;
}
else if (formatCh == 'P' || formatCh == 'p') {
style = GuidStyles.ParenthesisFormat;
}
else if (formatCh == 'X' || formatCh == 'x') {
style = GuidStyles.HexFormat;
}
// IFormattable interface
// We currently ignore provider
[System.Security.SecuritySafeCritical]
public String ToString(String format, IFormatProvider provider)
{
if (format == null || format.Length == 0)
format = "D";
string guidString;
int offset = 0;
bool dash = true;
bool hex = false;
if( format.Length != 1) {
// all acceptable format strings are of length 1
throw new FormatException(Environment.GetResourceString("Format_InvalidGuidFormatSpecification"));
}
char formatCh = format[0];
if (formatCh == 'D' || formatCh == 'd') {
guidString = string.FastAllocateString(36);
}
else if (formatCh == 'N' || formatCh == 'n') {
guidString = string.FastAllocateString(32);
dash = false;
}
else if (formatCh == 'B' || formatCh == 'b') {
guidString = string.FastAllocateString(38);
unsafe {
fixed (char* guidChars = guidString) {
guidChars[offset++] = '{';
guidChars[37] = '}';
}
}
}
else if (formatCh == 'P' || formatCh == 'p') {
guidString = string.FastAllocateString(38);
unsafe {
fixed (char* guidChars = guidString) {
guidChars[offset++] = '(';
guidChars[37] = ')';
}
}
}
else if (formatCh == 'X' || formatCh == 'x') {
guidString = string.FastAllocateString(68);
unsafe {
fixed (char* guidChars = guidString) {
guidChars[offset++] = '{';
guidChars[67] = '}';
}
}
dash = false;
hex = true;
}
else {
throw new FormatException(Environment.GetResourceString("Format_InvalidGuidFormatSpecification"));
}
unsafe {
fixed (char* guidChars = guidString) {
if (hex) {
// {0xdddddddd,0xdddd,0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}
guidChars[offset++] = '0';
guidChars[offset++] = 'x';
offset = HexsToChars(guidChars, offset, _a >> 24, _a >> 16);
offset = HexsToChars(guidChars, offset, _a >> 8, _a);
guidChars[offset++] = ',';
guidChars[offset++] = '0';
guidChars[offset++] = 'x';
offset = HexsToChars(guidChars, offset, _b >> 8, _b);
guidChars[offset++] = ',';
guidChars[offset++] = '0';
guidChars[offset++] = 'x';
offset = HexsToChars(guidChars, offset, _c >> 8, _c);
guidChars[offset++] = ',';
guidChars[offset++] = '{';
offset = HexsToChars(guidChars, offset, _d, _e, true);
guidChars[offset++] = ',';
offset = HexsToChars(guidChars, offset, _f, _g, true);
guidChars[offset++] = ',';
offset = HexsToChars(guidChars, offset, _h, _i, true);
guidChars[offset++] = ',';
offset = HexsToChars(guidChars, offset, _j, _k, true);
guidChars[offset++] = '}';
}
else {
// [{|(]dddddddd[-]dddd[-]dddd[-]dddd[-]dddddddddddd[}|)]
offset = HexsToChars(guidChars, offset, _a >> 24, _a >> 16);
offset = HexsToChars(guidChars, offset, _a >> 8, _a);
if (dash) guidChars[offset++] = '-';
offset = HexsToChars(guidChars, offset, _b >> 8, _b);
if (dash) guidChars[offset++] = '-';
offset = HexsToChars(guidChars, offset, _c >> 8, _c);
if (dash) guidChars[offset++] = '-';
offset = HexsToChars(guidChars, offset, _d, _e);
if (dash) guidChars[offset++] = '-';
offset = HexsToChars(guidChars, offset, _f, _g);
offset = HexsToChars(guidChars, offset, _h, _i);
offset = HexsToChars(guidChars, offset, _j, _k);
}
}
}
return guidString;
}
恩簡單的說就是有 D N P B X 這些模式可以用的感覺
GuidStyles.DigitFormat;
GuidStyles.NumberFormat;
GuidStyles.BraceFormat;
GuidStyles.ParenthesisFormat;
GuidStyles.HexFormat;
https://docs.microsoft.com/zh-tw/dotnet/api/system.guid.tostring?view=netcore-3.1
好 來TRYTRY
Guid G = Guid.NewGuid();
var D = G.ToString("D");//D:31b8c37e-434a-43c9-ba1c-554af19a4ce0
var N = G.ToString("N");//N:31b8c37e434a43c9ba1c554af19a4ce0
var B = G.ToString("B");//B:{31b8c37e-434a-43c9-ba1c-554af19a4ce0}
var P = G.ToString("P");//P:(31b8c37e-434a-43c9-ba1c-554af19a4ce0)
var X = G.ToString("X");//X:{0x31b8c37e,0x434a,0x43c9,{0xba,0x1c,0x55,0x4a,0xf1,0x9a,0x4c,0xe0}}
看完以後只覺得這5種模式 好像只有前兩種使用機會跟場景比較常有
這樣還是無法理解到底有什麼不一樣
直到這一篇文章說的
https://stackoverflow.com/questions/246930/is-there-any-difference-between-a-guid-and-a-uuid
原來沒有什麼不一樣
我原本以為會根據UUID的五個VERSION做不同的改變
Version 1 (MAC address)
Version 2 (DCE Security)
Version 3 (MD5 hash)
Version 4 (random)
Version 5 (SHA-1 hash) 只是生產的加密方法
都是根據https://www.ietf.org/rfc/rfc4122.txt
RFC4122做出的
然後微軟說所有的UUID都是GUID....恩
然後他呼叫的是底層的Ole32dll的CoCreateGuid
UuidCreate!!!!
https://doxygen.reactos.org/d4/d62/rpcrt4__main_8c.html#ad13d1a2fffd42b5f2f6c692b14b91af6
原來真的是一樣的 Version 4 !! random!!
另外一個可能不一樣的地方
不一樣的應該就是記憶體儲存方法.....
https://blog.gtwang.org/programming/difference-between-big-endian-and-little-endian-implementation-in-c/