1

I am reading some data.

byte[] data = inHandle.ReadBytes(int.MaxValue);

I want to be able to locate the index where the magic number for gzip (0x1f8b) starts. Is there a way to do it via linq?

2
  • 4
    Not meaning to be condescending but why use LINQ instead of standard array methodologies.. Array.IndexOf? Commented Mar 15, 2012 at 21:18
  • Just want to know if it can be done. I could just use a for loop as well. Commented Mar 15, 2012 at 21:22

2 Answers 2

2

I'm not sure if this is very efficient, but

byte[] data = new byte[]{ 1, 2, 3, 4, 0x1f, 0x8b, 5, 6 };

var indexedData = data.Select ((element,index) => new {element, index});

int? magicIndex = 
    (from d1 in indexedData
    from d2 in indexedData
    where d1.index == d2.index-1 && d1.element == 0x1f && d2.element == 0x8b
    select (int?)d1.index).SingleOrDefault ();

Console.WriteLine(magicIndex);

Which results in the index of 0x1f or null if it's not found.

Or

var magicNo = data.Zip( data.Skip(1), 
    (first, second) => first*256 + second).Select ((d,i) => new {d, i}).FirstOrDefault (d => d.d==0x1f8b);

if(magicNo != null)
{
    Console.WriteLine(magicNo.i);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Kinda cheating but if you can use the index:

var idx = data.Select( (b,index)=> new 
                { 
                   IsGzipStart =  b == 0x1f && data[index+1] == 0x8b, 
                   Index = index 
                }).FirstOrDefault(x => x.IsGzipStart);
Console.WriteLine(idx.Index);

I second the comment though - a simple loop is much clearer, readable and more efficient in this case.

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.