Ravi
Ravi

Reputation: 408

how to store byte string in byte[] using EntityFramework

I have

0x4D5A90000300000004000000FFFF0000B80000000000000040...

generated from sql server.

How can I insert byte string into byte[] column in database using EntityFramework?

Upvotes: 0

Views: 1361

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064114

As per my comment above, I strongly suspect that the best thing to do here is to return the data as a byte[] from the server; this should be fine and easy to do. However, if you have to use a string, then you'll need to parse it out - take off the 0x prefix, divide the length by 2 to get the number of bytes, then loop and parse each 2-character substring using Convert.ToByte(s, 16) in turn. Something like (completely untested):

int len = (value.Length / 2)-1;
var arr = new byte[len];
for(int i = 0; i < len;i++) {
    var s = value.Substring((i + 1) * 2, 2);
    arr[i] = Convert.ToByte(s, 16);
}

Upvotes: 2

Related Questions