Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Pythons UUID constructor yield a different result to the C# Guid constructor?

I am trying to write a Python utility to convert from Oracle's RAW byte strings (as strings) to Guids and vice-versa. I am trying to re-use the algorithm from the same utility I built in C#, but constructing a Guid and a UUID from the same byte array yields different Guid's/UUID's. They are the same aren't they? I've read that UUID is just a better term.

In C# I have a byte array, byte_array that looks like this:

{byte[16]}
    [0]: 151
    [1]: 163
    [2]: 6
    [3]: 235
    [4]: 224
    [5]: 173
    [6]: 188
    [7]: 79
    [8]: 182
    [9]: 220
    [10]: 222
    [11]: 173
    [12]: 29
    [13]: 103
    [14]: 37
    [15]: 125

and in Python I have a byte array that looks like this:

array('B', [151, 163, 6, 235, 224, 173, 188, 79, 182, 220, 222, 173, 29, 103, 37, 125])

These two arrays clearly have the same content, yet in C#:

guid = new Guid(byte_array) -> guid == eb06a397-ade0-4fbc-b6dc-dead1d67257d

And in python:

guid = uuid.UUID(bytes=byte_array.tostring()) -> guid == 97a306eb-e0ad-bc4f-b6dc-dead1d67257d

I see that C#'s Guid(...) does some re-arranging of the byte order, while Python's UUID(...) leaves the byte order in the UUID the same as in the byte array.

The Python result is incorrect for converting Oracle's RAW byte strings, as my C# code that works (it is in production in hundreds of lines of code), shows the correct Guid for a byte array.

Is there any way I get a UUID/Guid from a byte array in Python that is the same as a C# Guid for the same byte array?

I have looked at the source for the C# Guid(byte[] bytes) ctor, and while it is a simple assignment to positions in a Guid, the positions in the Guid are 11 in number and are of types, int, short, and byte, which I don't have in Python, and I don't want to get into bitwise manipulation to emulate C# types in Python.

like image 501
ProfK Avatar asked Aug 10 '26 07:08

ProfK


1 Answers

Just came across this issue as well... Solved it by using the bytes_le parameter in the constructor instead of bytes:

uuid.UUID(bytes_le = bytes)
like image 166
KW1 Avatar answered Aug 11 '26 20:08

KW1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!