简体   繁体   English

如何将GUID的一部分转换为long?

[英]How can I convert part of a GUID to a long?

Say I have this GUID: 说我有这个GUID:

57F67098-00A9-4F78-A729-4234F5AC512C

I only want to convert the last part ( 4234F5AC512C ) to a long in C#. 我只想在C#中将最后一部分( 4234F5AC512C )转换为long。

Try 尝试

long result = BitConverter.ToInt64(yourGuid.ToByteArray(), 8);

This will use the last eight bytes, not just the last six. 这将使用最后八个字节,而不仅仅是最后六个字节。 You can append & 0xFFFFFFFFFFFF if you want only six bytes. 如果只需要六个字节,则可以追加& 0xFFFFFFFFFFFF

Untested. 未经测试。 Check if byte order and endianess is as desired. 检查字节顺序和字节序是否符合要求。

Getting the last part of the string and then using the Convert.ToInt64 with the overload that accepts the base 16 for the conversion. 获取字符串的最后一部分,然后将Convert.ToInt64与接受基数为16的重载一起使用。

Guid g = new Guid("57F67098-00A9-4F78-A729-4234F5AC512C");
int pos = g.ToString().LastIndexOf('-');
string part = g.ToString().Substring(pos+1);
long result = Convert.ToInt64(part, 16);
Console.WriteLine(result.ToString());

That's a hex (base 16) value. 这是一个十六进制(以16为底)的值。 You can convert it this way Convert.ToInt64("4234F5AC512C", 16) . 您可以通过Convert.ToInt64("4234F5AC512C", 16)

http://msdn.microsoft.com/en-us/library/bb311038.aspx http://msdn.microsoft.com/en-us/library/bb311038.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM