English 中文(简体)
1. 编写《指南》。
原标题:Create a Guid from an int
  • 时间:2010-07-21 21:00:12
  •  标签:
  • c#
  • guid

鉴于<条码>int,你如何反复制定同样的准则?

Edit:
I m integrating two systems, one uses ints as a primary key, the other recognises an object by it s Guid. Because of this, I need to be able to recreate the same Guid for a given int so that the second system can recognise the entities coming from the first.

最佳回答

如果你根据分类编制指南,它就不再是全球独特的识别特征。

说到,你可以使用进行分类的构造和其他一些参数:

int a = 5;
Guid guid = new Guid(a, 0, 0, new byte[8]);

并非真正可取......

问题回答

这是对Version 35的指导的完美使用。

有五种指导:

  • 1: time based version (UuidCreateSequential)
  • 2: DCE Security version, with embedded POSIX UIDs
  • 3: Name-based version that uses MD5 hashing
  • 4: Randomly or pseudo-randomly generated version (UuidCreate)
  • 5: Name-based version that uses SHA-1 hashing

第3版和第5版的设计是任意的 > 姓名,总有相同的guid地图。

想法是任意“<>><> > >>和“<>>>>>> 空间<>/em> ”,并在概念上将其储存在一个指导结构中:

"CustomersTable" + "1"  -> sha1() -> Guid
"CustomersTable" + "2"  -> sha1() -> Guid

您避免与> 即号”相撞:

"InvoicesTable" + "1"   -> sha1() -> Guid
"InvoicesTable" + "2"   -> sha1() -> Guid

具体来说,“名称空间”不是指指号,而是指指:

Namespace_Customer =  {277D668B-264A-433E-9EEB-867C02C6D48D} ;
Namespace_Invoice =   {D8024CFB-D6F8-4DB2-9135-96FD0A7B97AB} ;

http://www.apps.ietf.org/rfc/rfc4122.html#page-30" 给出四个预先界定的名称:

NameSpace_DNS =   {6ba7b810-9dad-11d1-80b4-00c04fd430c8} ;
NameSpace_URL =   {6ba7b811-9dad-11d1-80b4-00c04fd430c8} ;
NameSpace_OID =   {6ba7b812-9dad-11d1-80b4-00c04fd430c8} ;
NameSpace_X500 =  {6ba7b814-9dad-11d1-80b4-00c04fd430c8} ;

辩论会描述了如何将16个 has子带入指导结构。 如果您的头16位牧师是:

hex: 00010203040506070809101112131415

这些结构分为:

                timeLow = 00 01 02 03
                timeMid = 04 05
     timeHighAndVersion = ((06 07) & 0x0FFF) | (0x5000)  //high 4-bits are version, 5=SHA1, 3=MD5
  clockSeqHiAndReserved = (08 & 0x3F) | 0xD0    //set high bits to 10
            clockSeqLow = 09
                   node = 10 11 12 13 14 15

通过我的<代码>NameToGuid,管理客户和发票:

NameToGUIDA(Namespace_Customer, "1") =  {7E378BDB-5BB1-5EBF-9C63-E00AC1AEEDB7}
NameToGUIDA(Namespace_Customer, "2") =  {830F19D8-D5DC-5321-8CF9-D708660BE473}
NameToGUIDA(Namespace_Customer, "10") = {C052F6ED-0334-5827-B077-7F4C28B78223}

NameToGUIDA(Namespace_Invoice, "1") =   {BB5408DA-B99C-5816-B144-A63CC02BB21C}
NameToGUIDA(Namespace_Invoice, "2") =   {73DDD3B3-1139-50C8-9B0D-067145611346}
NameToGUIDA(Namespace_Invoice, "10") =  {AE1799A8-620F-55DB-AC85-D7056B70AFB5}

如果你想要由just<>/em>组成,则由以下人员组成: 当时:

int x = 5;
string padded = x.ToString().PadLeft(32, 0 );
var intGuid = new Guid(padded);

intGuid.ToString(); // 00000000-0000-0000-0000-000000000005

您可以将64英寸转换成星号,并利用建筑商超负荷这样做:

var guid = new Guid(byteArray);

Edit - oops。 它以128个轨道为基础,而不是64个。 如果您有某种习俗,即MyBigInt(一种128条轨道)的话,那么你就可以这样做。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...