Byte array search in another byte array (if not present search for first part of the searchfor byte array) in c#

Locked Byte array search in another byte array (if not present search for first part of the searchfor byte array) in c#

  • Friday, August 31, 2012 9:55 AM
     
     

    Hi friends,

    I have to search in a byte array a constant byte array . If it is not found then i have to search for part of byte array

    Thanks in advance

    Leenu


    Leenu Thomas



    • Edited by Leenu Friday, August 31, 2012 9:57 AM
    • Edited by Leenu Friday, August 31, 2012 9:58 AM
    •  

All Replies

  • Friday, August 31, 2012 11:15 AM
     
     
    It's the same as a string search..
  • Friday, August 31, 2012 11:43 AM
     
     Answered Has Code

    Hi.

    Try this

    byte[] source = new byte[100];
                byte[] src = new byte[10];
    
                string strSource = Encoding.Unicode.GetString(source);
                string strSrc = Encoding.Unicode.GetString(src);
    
                while ((strSource.IndexOf(strSource) < 0) && (strSrc.Length > 0))
                {
                    strSrc = strSrc.Substring(0, strSrc.Length - 1);
                }
    
                byte[] res = Encoding.Unicode.GetBytes(strSrc);


    Regards,
    Bubu
    http://zsvipullo.blogspot.it

    Please mark my answer if it helped you, I would greatly appreciate it.

  • Friday, August 31, 2012 12:27 PM
     
     

    I had the same idea first, but an arbitrary byte stream is not necessarily a valid uni-code encoding. Thus GetString() can fail with an exception. Also I'm not sure about the UTF-8 padding, whether the byte stream character mapping is bijective. When it's not, then you may get collisions in the string representation.

    I also thought about an ASCII encoding, but that's only 7bit..

  • Sunday, September 02, 2012 12:29 PM
     
     

    That sounds like pattern matching. If so something from here might help.

    Note that most of pattern matching literature refers to text and uses strings as examples, but the algorithms apply to byte arrays.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.