-1

I have stored files in database in a column of varbinary type. Files look something like .. "0xFFD8FFE......." in the database. Before downloading these files I want to first check the first n characters such as first eight characters to find out if they match "0xFFD8FF" if they do I will save the file as ".jpg" and so on. How can i do so??I tried many things such as

Byte[] threeBytes = new Byte[] { objData[0], objData[1], objData[2], objData[3], objData[5] };      if (objTable.Rows[0]["img"].ToString().StartsWith("0xFFD"))
    {
        strFileToSave += ".jpg";
    }

But I am unable to compare them.

SamuraiJack
  • 5,131
  • 15
  • 89
  • 195

2 Answers2

1

If I understand correctly you need to compare first n chars from bytes. You can use SequenceEqual to achieve it.

var firstNBytes = bytesFromDatabase.Take(n);
var bytesToCompare = new byte[]{ 0,1,2};//Whatever build your bytes

bool equal = firstNBytes.SequenceEqual(bytesToCompare);
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • I am not sure if i can compare byte stream with "0xFFD8FFE" with your code..I just want to check if the first n characters(as they appear in database) are same as "0xFFD8FFE"? – SamuraiJack Oct 12 '13 at 18:24
  • What do you meant by byte stream? try my code, that should work – Sriram Sakthivel Oct 12 '13 at 18:37
  • I am unable to understand `var bytesToCompare = new byte[]{ 0,1,2};` What am i supposed to replace it with? – SamuraiJack Oct 12 '13 at 18:41
  • For example if you'd like to compare "SomeText" with first 8 characters then `var bytesToCompare = Encoding.ASCII.GetBytes("SomeText");` – Sriram Sakthivel Oct 12 '13 at 18:48
  • I tried your code but its returning false although the file do starts with `"0xFFD8FFE"` in the database. I tried both `var bytesToCompare = Encoding.ASCII.GetBytes("0xFFD8FFE");` and `var bytesToCompare = Encoding.Unicode.GetBytes("0xFFD8FFE");` – SamuraiJack Oct 12 '13 at 18:56
  • `0xFFD8FFE` is not a string. it looks like hex number. I'll suggest you to read the file from DB, and look and first 8 chars from it using Debugger, then use those chars – Sriram Sakthivel Oct 12 '13 at 19:07
  • @Arbaaz Forget about strings and characters. It is *bytes* which you need to look at, as Sriram showed. – Andrew Morton Oct 12 '13 at 22:42
0

This answer is meant to augment the one posted by @Sriram.

To OP: Here is how you can get bytesToCompare:

Dim bytesToCompare As Byte() = "FF-D8-FF".Split("-"c).
             Select(Function(n) Convert.ToByte(Convert.ToInt32(n, 16))).ToArray()

(Based on this answer)

Or do it the other way around, convert input bytes to hex string and compare:

Dim hex As String = "0x" & ByteArrayToString(firstNBytes)
If hex = "0xFFD8FF" Then ...

Public Function ByteArrayToString(ba As Byte()) As String
  Dim hex As New StringBuilder(ba.Length * 2)
  For Each b As Byte In ba
    hex.AppendFormat("{0:x2}", b)
  Next
  Return hex.ToString()
End Function

Implementation of ByteArrayToString was converted from C# to VB.NET (personal preference).

Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151