10

Given some data stored in a SQL binary field:

0x83C8BB02E96F2383870CC1619B6EC...

I'd like to convert it into a byte array, but it doesn't seem like I can just cast this directly into a byte like so:

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

What am I missing here?

5
  • Uh? Querying the DB for a binary field will return a byte array. Commented Oct 12, 2010 at 15:07
  • What do you have right now? Literal text? A string? Commented Oct 12, 2010 at 15:08
  • Say I'm in Query Analyzer, and I SELECT from a binary field, the result grid has the value from above (I truncated the example because it's over 300 characters long). Commented Oct 12, 2010 at 15:10
  • Why is there binary data in the database? Commented Oct 12, 2010 at 15:11
  • 2
    Because that's how I inherited it. Commented Oct 12, 2010 at 15:11

3 Answers 3

5

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

If you just want to copy/paste the hexadecimal data into your C# code (as you seem to have written), then you'll need to convert it from hexadecimal to binary. .NET provides a (rather obscure) class for this sort 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");
}

If your binary data is coming from your database in a different form than a copy/pasted string, you'll need to provide more information about your setup.

Sign up to request clarification or add additional context in comments.

3 Comments

Removing the 0x from the string is a subtle but important difference between the data that would be copied and the example here. Without doing that you will get xsd:hexBinary errors
I'm getting xsd:hexBinary invalid errors even after removing the 0x. Edit: removing one character from the end (to make the length even, I suppose) fixed it.
Is there a way to do this in .NET Core? I can't find an analogous library
4

Your casting is wrong.

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

I had the same problem and I stumbled across this question. After further research I tried casting like the above and it worked.

1 Comment

I'm getting error "Integral constant is too large". Any ideas?
0

Stolen from this answer:

Remove the 0x from the beginning of your data and create a string:

var stringHex = "83C8BB02E96F2383870CC1619B6EC...";

Then convert to byte[] with this method:

byte[] myArray = Enumerable.Range(0, stringHex.Length / 2)
                           .Select(x => Convert.ToByte(stringHex.Substring(x * 2, 2), 16))
                           .ToArray();

I had to use this method because my data was too long, so I was getting errors about Int32 and Int64 overflow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.