How to generate 8 bytes unique id from GUID?

后端 未结 10 1672
长情又很酷
长情又很酷 2020-12-04 01:21

I try to use long as unique id within our C# application (not global, and only for one session) for our events. Do you know if the following will generate an unique long id?

10条回答
  •  清歌不尽
    2020-12-04 02:16

    var s = Guid.NewGuid().ToString();
    var h1 = s.Substring(0, s.Length / 2).GetHashCode(); // first half of Guid
    var h2 = s.Substring(s.Length / 2).GetHashCode(); // second half of Guid
    var result = (uint) h1 | (ulong) h2 << 32; // unique 8-byte long
    var bytes = BitConverter.GetBytes(result);
    

    P. S. It's very good, guys, that you are chatting with topic starter here. But what about answers that need other users, like me???

提交回复
热议问题