Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Guid.ToByteArray() cross-platform?

Tags:

c#

.net

guid

I want to store Guids in a database which does not support Guid/uniqueidentifier data type, therefore I convert Guid to byte array using .ToByteArray() method. However, this method converts value in a strage way:

11223344-5566-7788-9900-AABBCCDDEEFF
will become
44, 33, 22, 11, 66, 55, 88, 77, 99, 00, AA, BB, CC, DD, EE, FF

As I understand, this is because of endian ordering.

I would like to know if this method will return the same result on every platform (86x hardware, 64x hardware, Linux, Windows etc) and there will be no changes in byte order no matter on which platform I run my software.

like image 878
user1613797 Avatar asked Jun 06 '14 12:06

user1613797


1 Answers

For your question:

I would like to know if this method will return the same result on every platform (86x hardware, 64x hardware, Linux, Windows etc)

Yes it will be same for all the platforms.

However, this method converts value in a strage way:

The order returned from ToByteArray would be different from string representation.

See: Guid.ToByteArray Method

Note that the order of bytes in the returned byte array is different from the string representation of a Guid value. The order of the beginning four-byte group and the next two two-byte groups is reversed, whereas the order of the last two-byte group and the closing six-byte group is the same.

like image 73
Habib Avatar answered Nov 20 '22 00:11

Habib