0

reference to converting sql server rowversion to long or ulong? i can convert SQL RowVersion to ulong by code:

static ulong BigEndianToUInt64(byte[] bigEndianBinary)
{
    return ((ulong)bigEndianBinary[0] << 56) |
           ((ulong)bigEndianBinary[1] << 48) |
           ((ulong)bigEndianBinary[2] << 40) |
           ((ulong)bigEndianBinary[3] << 32) |
           ((ulong)bigEndianBinary[4] << 24) |
           ((ulong)bigEndianBinary[5] << 16) |
           ((ulong)bigEndianBinary[6] <<  8) |
                   bigEndianBinary[7];
}

Now, I am faced with a problem, how to convert ulong to byte [8]? I save the value of rowversion to a file, then read it and use it to make the query. the query parameter should be byte[] , not ulong . otherwise , there is an error will be raised.

goldii
  • 242
  • 2
  • 18
  • Are you using MS SQL Server? – jarlh May 17 '21 at 09:11
  • If you're using .NET Core 2.1+, you can use `BinaryPrimitives.ReadUInt64BigEndian` instead of that custom method, and use `BinaryPrimitives.WriteUInt64BigEndian` to go the other way – canton7 May 17 '21 at 09:11
  • @canton7 , thanks . i'm using .net 4.5. – goldii May 17 '21 at 11:20
  • Support for .NET Framework 4.5 ended in **2016**! Get yourself onto something which at least has security fixes! – canton7 May 17 '21 at 12:03
  • @canton7. yes it supports. but , it's not a good idea adding too much dll to the project while we just need only two functions. – goldii May 17 '21 at 14:10

1 Answers1

0

i got an answer from BigEndian.GetBytes Method (Int64)

thanks all.

    public static byte[] GetBytes(this ulong value)
    {
        return new[]
        {
            (byte)(value >> 56),
            (byte)(value >> 48),
            (byte)(value >> 40),
            (byte)(value >> 32),
            (byte)(value >> 24),
            (byte)(value >> 16),
            (byte)(value >> 8),
            (byte)(value)
        };
    }
goldii
  • 242
  • 2
  • 18