How do I find a string in byte array in C#

Started by
3 comments, last by feal87 14 years, 11 months ago
I have an array of bytes that contains data. I would like to search for a specific string in the array of bytes. I dont know really how to do this in C# byte[] dataArray = new byte[1000]; String searchString = "Graphics"; How do I find the first occurance of the word "Graphics" in the array dataArray, and get an index location where the string begins (where the 'G' would be located in dataArray) Thanks
Advertisement
Explain better your question, what kind of encoding is used for the string inside the bytearray? Cause that its prominent to detect the correct position and to decode the string.

Anyway have a look at the System.Text namespace especially the ASCIIEncoding and UnicodeEncoding class.
the dataArray is of type 'byte' so I guess that is an unsigned value 0-255.

Im used to c where you work with 'char' and a string is an array of 'char'

So do I need to somehow turn my dataArray from byte to char?
Solved it, I found a way to turn the 'byte' array into a string, thanks for the help.
Quote:Original post by SelethD
the dataArray is of type 'byte' so I guess that is an unsigned value 0-255.

Im used to c where you work with 'char' and a string is an array of 'char'

So do I need to somehow turn my dataArray from byte to char?


Not really, if it is encoded in unicode UTF-16 2 bytes are 1 char UTF-32 4 bytes are 1 char.
If it work with only 1 byte per char it means you are using an ASCII coding.

In C there is no notion of anything other than the ASCII encoding, that's the reason.

This topic is closed to new replies.

Advertisement