Created
October 16, 2017 11:45
-
-
Save novabyte/672ec549635eaccbc401b442d7a52bd8 to your computer and use it in GitHub Desktop.
A helper class to convert UUIDv4 strings into bytes and back again with C#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class UUID | |
{ | |
public static byte[] FromString(string uuid) | |
{ | |
var guid = new Guid(uuid).ToByteArray(); | |
Array.Reverse(guid, 6, 2); | |
Array.Reverse(guid, 4, 2); | |
Array.Reverse(guid, 0, 4); | |
return guid; | |
} | |
public static string FromBytes(byte[] uuid) | |
{ | |
Array.Reverse(uuid, 0, 4); | |
Array.Reverse(uuid, 4, 2); | |
Array.Reverse(uuid, 6, 2); | |
return new Guid(uuid).ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: