Jump to content
  • Advertisement
Sign in to follow this  
SelethD

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

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!