C# 取 UUID
C# 取 UUID
資料來源: https://docs.microsoft.com/zh-tw/dotnet/api/system.guid.newguid?view=net-6.0
https://www.delftstack.com/zh-tw/howto/csharp/generate-uuid-in-csharp/
Code
// Create and display the value of two GUIDs. Guid g = Guid.NewGuid(); Console.WriteLine(g); Console.WriteLine(Guid.NewGuid()); // This code example produces a result similar to the following: // 0f8fad5b-d9cb-469f-a165-70867728950e // 7c9e6679-7425-40de-944b-e07fc1f90ae7
Guid myUUId = Guid.NewGuid(); string convertedUUID = myUUId.ToString(); Console.WriteLine("Current UUID is: " + convertedUUID);
3 thoughts on “C# 取 UUID”
C# 64BYTE UUID [ChatGPT]
C# 線上編譯器: https://dotnetfiddle.net/
In C#, you can generate a 64-byte UUID (Universally Unique Identifier) using the Guid structure. A GUID is a 128-bit value consisting of one group of 8 hexadecimal digits, followed by three groups of 4 hexadecimal digits each, followed by one group of 12 hexadecimal digits, for a total of 32 hexadecimal digits (representing 16 bytes), separated by hyphens.
To generate a 64-byte UUID using Guid, you can convert the GUID to a byte array and then concatenate the resulting arrays to obtain a 64-byte array. Here’s an example:
在 C# 中,您可以使用 Guid 結構生成一個 64 字節的 UUID(通用唯一標識符)。 GUID 是一個 128 位的值,由一組 8 個十六進制數字組成,然後是三組,每組 4 個十六進制數字,然後是一組 12 個十六進制數字,總共 32 個十六進制數字(代表 16 個字節),分隔 通過連字符。
要使用 Guid 生成 64 字節的 UUID,您可以將 GUID 轉換為字節數組,然後連接生成的數組以獲得 64 字節的數組。 這是一個例子:
using System;
public class Program
{
public static void Main()
{
Guid guid = Guid.NewGuid();
byte[] guidBytes = guid.ToByteArray();
byte[] result = new byte[64];
Buffer.BlockCopy(guidBytes, 0, result, 0, 16);
Buffer.BlockCopy(guidBytes, 0, result, 16, 16);
Buffer.BlockCopy(guidBytes, 0, result, 32, 16);
Buffer.BlockCopy(guidBytes, 0, result, 48, 16);
Console.WriteLine(BitConverter.ToString(result).Replace("-", ""));
}
}
結果01: 6BC1F311507D8140A9497053126A49A56BC1F311507D8140A9497053126A49A56BC1F311507D8140A9497053126A49A56BC1F311507D8140A9497053126A49A5
結果02: 730FE116520BC547ABD5C099AFEFCF37730FE116520BC547ABD5C099AFEFCF37730FE116520BC547ABD5C099AFEFCF37730FE116520BC547ABD5C099AFEFCF37
C#根據時間產生有序的GUID編碼
https://www.cnblogs.com/shiningrise/p/5690016.html
using System;
public class Program
{
public static Guid OrderlyGuid()
{
byte[] guidArray = Guid.NewGuid().ToByteArray();
var baseDate = new DateTime(1900, 1, 1);
DateTime now = DateTime.Now;
var days = new TimeSpan(now.Ticks - baseDate.Ticks);
TimeSpan msecs = now.TimeOfDay;
byte[] daysArray = BitConverter.GetBytes(days.Days);
byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
Array.Reverse(daysArray);
Array.Reverse(msecsArray);
Array.Copy(daysArray,daysArray.Length - 2,guidArray,guidArray.Length - 6,2);
Array.Copy(msecsArray,msecsArray.Length - 4,guidArray,guidArray.Length - 4,4);
return new Guid(guidArray);
}
public static void Main()
{
Guid guid = OrderlyGuid();
byte[] guidBytes = guid.ToByteArray();
byte[] result = new byte[64];
Buffer.BlockCopy(guidBytes, 0, result, 0, 16);
Buffer.BlockCopy(guidBytes, 0, result, 16, 16);
Buffer.BlockCopy(guidBytes, 0, result, 32, 16);
Buffer.BlockCopy(guidBytes, 0, result, 48, 16);
Console.WriteLine(BitConverter.ToString(result).Replace("-", ""));
}
}
569A28360A95694C9BF4AFCD00264D3F569A28360A95694C9BF4AFCD00264D3F569A28360A95694C9BF4AFCD00264D3F569A28360A95694C9BF4AFCD00264D3F
EB661445648DB04D99B5AFCD002662E7EB661445648DB04D99B5AFCD002662E7EB661445648DB04D99B5AFCD002662E7EB661445648DB04D99B5AFCD002662E7
7C031176A011ED418089AFCD002679D67C031176A011ED418089AFCD002679D67C031176A011ED418089AFCD002679D67C031176A011ED418089AFCD002679D6
C# UUID to DB SID
public static string UUID2SID(int len=32)
{
String StrResult = "";
Guid guid = Guid.NewGuid();
byte[] guidBytes = guid.ToByteArray();
byte[] result = new byte[64];
Buffer.BlockCopy(guidBytes, 0, result, 0, 16);
Buffer.BlockCopy(guidBytes, 0, result, 16, 16);
Buffer.BlockCopy(guidBytes, 0, result, 32, 16);
Buffer.BlockCopy(guidBytes, 0, result, 48, 16);
StrResult = BitConverter.ToString(result).Replace("-", "");
if(len<=128)
{
StrResult = StrResult.Substring(0,len);
}
return StrResult;
}
//---
//test code
String StrUUID = "";
StrUUID = SQLDataTableModel.UUID2SID(16);
StrUUID = SQLDataTableModel.UUID2SID(32);
StrUUID = SQLDataTableModel.UUID2SID(64);
StrUUID = SQLDataTableModel.UUID2SID(128);
//---test code