Reputation: 29
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
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
Reputation: 3883
try this
String rowversion = "0x000000000001265E";
byte[] mybyteArray = Convert.FromBase64String(rowversion);
Upvotes: 2
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