Apologies if this is a duplicate and I'm terrible at searching. I'll post a few of the things I tried below.
I'm using Entity Framework to pull a varbinary column from a SQL Server database. Entity Framework returns a byte[]
.
When I query the database using SSMS, this is what I get (and what I'm trying to get in C#): 0x540FA51A000706010096000B64FFAEC1
Database encoding is UTF8. Thanks in advance.
Here are a few of the things I've tried:
public static string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
public static string ByteArrayToString(byte[] ba)
{
if (BitConverter.IsLittleEndian)
Array.Reverse(ba);
string hex = BitConverter.ToString(ba);
return hex.Replace("-", "");
}
private static string ByteArrayToHex(byte[] barray)
{
char[] c = new char[barray.Length * 2];
byte b;
for (int i = 0; i < barray.Length; ++i)
{
b = ((byte)(barray[i] >> 4));
c[i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30);
b = ((byte)(barray[i] & 0xF));
c[i * 2 + 1] = (char)(b > 9 ? b + 0x37 : b + 0x30);
}
return new string(c);
}