Convert byte array represented for sql_variant typed column value to long/int64

194 views Asked by At

I have a byte array value of 10 elements taken from an sql_variant column in my database table and would like to convert it to a long value. I tried this.

 byte []a = new byte[10]{ 127, 1, 0, 202, 154, 59, 0, 0, 0, 0 };
 long i = BitConverter.ToInt64(a, 0);
 Console.WriteLine("{0}", i);

but it's not giving me the correct result. I expect it to be 10000000000.

Thank you if you could offer me some ideas of classes or methods in C# I can look up into.

1

There are 1 answers

0
Oleg Belousov On

10000000000 in a hex notation is 02540BE400.

Byte array for this value in reverse order is:

byte[] a = new byte[10] { 0x00, 0xE4, 0x0B, 0x54, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00};

or in a decimal notation:

byte[] a = new byte[10] { 0, 228, 11, 84, 2, 0, 0, 0, 0, 0 };

For this array your function returns expected result: 10000000000.