Domanda

Say I have this GUID:

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

I only want to convert the last part (4234F5AC512C) to a long in C#.

È stato utile?

Soluzione

Getting the last part of the string and then using the Convert.ToInt64 with the overload that accepts the base 16 for the conversion.

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());

Altri suggerimenti

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.

Untested. Check if byte order and endianess is as desired.

That's a hex (base 16) value. You can convert it this way Convert.ToInt64("4234F5AC512C", 16).

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top