Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a code in c#, for searching a specific pattern, say 01111110, from the data.
Any help will be appreciated.

Regards
Posted
Updated 6-Jul-11 21:17pm
v2
Comments
Dalek Dave 7-Jul-11 3:17am    
Edited for Grammar and Readability.
BobJanova 7-Jul-11 6:07am    
Please be more clear about what you are actually wanting to do, in particular whether the patterns may cross byte boundaries. For example, should the byte sequence [07, E2] – 0000 0111 1110 0010 – match?

As far as I understand, you are not looking for a specific byte matching your pattern (that would be trivial, searching byte[] array), but your need to match pattern anywhere in the data, maybe crossing byte boundaries? If so, I can only offer you to scan each byte of data for the bit your pattern starts with, then compare next bit (maybe taken from the next byte, if you must cross byte boundaries) with the second bit of a pattern and so on. Definitely this - very straightforward - algorithm may be improved. Check algorithms for substring search, they may be applied here
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Jul-11 3:20am    
Agree, my 5 (I'm not sure OP formulated the problem correctly though). I would note: algorithms for substring search should be applied (most certainly) but never the implementations as they are only applied to characters, not bits. However, relatively simple modification of such algorithms to bit patterns can be quite feasible.
--SA
Timberbird 7-Jul-11 3:49am    
Thank you. Well, I thought noone would care to post a question regarding simple byte search :). On the other hand, there may be ready-to-use tools for pattern search - for example, in streams; unfortunately, I don't know of any
Sergey Alexandrovich Kryukov 8-Jul-11 1:59am    
I'm not sure there are tools searching a bit pattern across byte boundaries as you suggested. They can be obtained via modification of existing pattern search algorithms. The fact that they are readily available and we all use them does not make them trivial -- I recently saw descriptions of some -- quite brain-bending :-)
--SA
you can convert the 8-bit pattern into a byte, then you can transform the next
8-bits into a byte and compare it...
 
Share this answer
 
Comments
Dalek Dave 7-Jul-11 3:18am    
Nice idea, but a little ungainly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900