Helen
Helen

Reputation: 29

How to convert a rowversion in binary string format to byte array

i need to convert the string representation of rowversion column in sql server 2008 to byte array using c#

String rowversion = "0x000000000001265E";

i want to convert this to byte[]

any idea ?

Upvotes: 1

Views: 5458

Answers (3)

Simon Miller
Simon Miller

Reputation: 970

using System;
using System.Linq;    
byte[] RowVersionConverter(string rowVersion)
    {
       var value = ulong.Parse(rowVersion.SubString(2), System.Globalization.NumberStyles.HexNumber);
       return BitConverter.GetBytes(value);
    }

This might work for you. But I think it may well be in the reverse order of how SQL will expect it. So you may want this:

byte[] RowVersionConverter(string rowVersion)
{
   var value = ulong.Parse(rowVersion.SubString(2), System.Globalization.NumberStyles.HexNumber);
   return BitConverter.GetBytes(value).Reverse().ToArray();
}

Upvotes: 0

Amir Ismail
Amir Ismail

Reputation: 3883

try this

String rowversion = "0x000000000001265E";
byte[] mybyteArray = Convert.FromBase64String(rowversion);

Upvotes: 2

Ovais Khatri
Ovais Khatri

Reputation: 3211

char[] chrArr = rowVersion.ToCharArray();

byte[] objBytes = new byte[charArr.Length];

for(int x=2;x<charArr.Length;x++)
{
objBytes[i] = (byte)charArr[x];

}

Upvotes: 0

Related Questions