Search code examples
c#arrays.netguiddata-conversion

How to implement GUID toByteArray?


How does Guid.ToByteArray() work in c#.Can someone help me understand how does the following Guid token gets converted to ByteArray.

 Guid: 35918bc9-196d-40ea-9779-889d79b753f0
 guid.toByteArray: C9 8B 91 35 6D 19 EA 40 97 79 88 9D 79 B7 53 F0

Solution

  • A guid is essentially just a 128-bit number. Internally this is represented as one 32-bit int, two 16-bit ints and eight 8-bit ints.

    So conversion to a byte array is essentially just creating an array, and using shifting to select the correct byte in the 16 & 32-bit ints.