From ce75909b8bde39583e833115f547ab78043ab72d Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Mon, 25 Aug 2025 17:00:36 -0700 Subject: [PATCH] Bitwise Guid helpers --- util/Guid.cs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 util/Guid.cs diff --git a/util/Guid.cs b/util/Guid.cs new file mode 100644 index 0000000..413ad06 --- /dev/null +++ b/util/Guid.cs @@ -0,0 +1,81 @@ + + + +using System; + +namespace lib; + +public static class Guid +{ + public static System.Guid GuidFromLongs( long a, long b ) + { + byte[] guidData = new byte[16]; + Array.Copy( BitConverter.GetBytes( a ), guidData, 8 ); + Array.Copy( BitConverter.GetBytes( b ), 0, guidData, 8, 8 ); + return new System.Guid( guidData ); + } + + public static (long, long) ToLongs( this System.Guid guid ) + { + var bytes = guid.ToByteArray(); + var long1 = BitConverter.ToInt64( bytes, 0 ); + var long2 = BitConverter.ToInt64( bytes, 8 ); + return (long1, long2); + } + + public static System.Guid GuidFromULongs( ulong a, ulong b ) + { + byte[] guidData = new byte[16]; + Array.Copy( BitConverter.GetBytes( a ), guidData, 8 ); + Array.Copy( BitConverter.GetBytes( b ), 0, guidData, 8, 8 ); + return new System.Guid( guidData ); + } + + public static (ulong, ulong) ToULongs( this System.Guid guid ) + { + var bytes = guid.ToByteArray(); + var ulong1 = BitConverter.ToUInt64( bytes, 0 ); + var ulong2 = BitConverter.ToUInt64( bytes, 8 ); + return (ulong1, ulong2); + } + + public static System.Guid GuidFromInts( int a, int b, int c, int d ) + { + byte[] guidData = new byte[16]; + Array.Copy( BitConverter.GetBytes( a ), guidData, 4 ); + Array.Copy( BitConverter.GetBytes( b ), 0, guidData, 4, 4 ); + Array.Copy( BitConverter.GetBytes( c ), 0, guidData, 8, 4 ); + Array.Copy( BitConverter.GetBytes( d ), 0, guidData, 12, 4 ); + return new System.Guid( guidData ); + } + + public static (int, int, int, int) ToInts( this System.Guid guid ) + { + var bytes = guid.ToByteArray(); + var a = BitConverter.ToInt32( bytes, 0 ); + var b = BitConverter.ToInt32( bytes, 4 ); + var c = BitConverter.ToInt32( bytes, 8 ); + var d = BitConverter.ToInt32( bytes, 12 ); + return (a, b, c, d); + } + + public static System.Guid GuidFromUInts( uint a, uint b, uint c, uint d ) + { + byte[] guidData = new byte[16]; + Array.Copy( BitConverter.GetBytes( a ), guidData, 4 ); + Array.Copy( BitConverter.GetBytes( b ), 0, guidData, 4, 4 ); + Array.Copy( BitConverter.GetBytes( c ), 0, guidData, 8, 4 ); + Array.Copy( BitConverter.GetBytes( d ), 0, guidData, 12, 4 ); + return new System.Guid( guidData ); + } + + public static (uint, uint, uint, uint) ToUInts( this System.Guid guid ) + { + var bytes = guid.ToByteArray(); + var a = BitConverter.ToUInt32( bytes, 0 ); + var b = BitConverter.ToUInt32( bytes, 4 ); + var c = BitConverter.ToUInt32( bytes, 8 ); + var d = BitConverter.ToUInt32( bytes, 12 ); + return (a, b, c, d); + } +}