Convert SQL to Binary Array

Given some of the data stored in the SQL binary field:

0x83C8BB02E96F2383870CC1619B6EC...

I would like to convert it to an array of bytes, but it doesn't look like I can just pass this directly to bytes, for example:

byte[] someBytes = (byte) 0x83C8BB02E96F2383870CC1619B6EC...;

What am I missing here?

+3
source share
1 answer

The data stored in the SQL field is in binary format. The string '0x83 ..' that you are quoting is just a hexadecimal representation of this binary data.

If you just want to copy / paste the hex data into your C # code (as you seem to have written), you will need to convert it from hex to binary..NET provides a (rather obscure) class for this kind of thing:

using System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary

public byte[] ToBytes(string hex)
{
    var shb = SoapHexBinary.Parse(hex);
    return shb.Value;
}

public void TestConvert()
{
    byte[] someBytes = ToBytes("83C8BB02E96F2383870CC1619B6EC");
}

, / , .

+3

Source: https://habr.com/ru/post/1769170/