Zeul1 Posted November 1, 2006 Posted November 1, 2006 I am in the process of upgrading my VB6 app to .net I make extensive use of InstrB for byte searching within an array of bytes eg: Dim test(3) as byte test(2) = 1 test(3) = &HB3 r = instrB(offset, bArray, test) Now i see there is no intrinsic equivalent in .net or is there? Are there any functions that can be used for byte searching? Many thanks Quote
Administrators PlausiblyDamp Posted November 2, 2006 Administrators Posted November 2, 2006 If you have a byte array you could just use the Array.IndexOf() method to search the array. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Zeul1 Posted November 2, 2006 Author Posted November 2, 2006 Please could you elaborate. Can IndexOf() be used to search for a byte sequence instead of one element? The test i ran always resulted in -1. For example in the first post i would be searching for a byte sequence of 00 00 01 B3, with variable start offsets. Quote
Leaders snarfblam Posted November 2, 2006 Leaders Posted November 2, 2006 IndexOf will only find a match for a single byte. We aren't really supposed to give code handouts here because it tends to discourage thinking, but I was bored so I whipped this up. 'This function searches by using the Array.IndexOf method to find a match for the first 'item in the search array and then looping through the rest of the search array testing for a match. 'This is repeated until either a match is found or the end of the array is reached. Public Function SearchArray(Of T)(ByVal array As T(), ByVal search As T(), ByVal start As Integer) As Integer 'In the event of empty or null arrays return no match If array Is Nothing Or search Is Nothing Or array.Length = 0 Or search.Length = 0 Then _ Return -1 'Looping variables Dim index As Integer = start - 1 'Where we will begin searching Dim match As Boolean = False 'Will be set to true when a match is found While Not match 'Find the index where we have a match for the first item in the search array 'starting from 'start' or after our last match index = System.Array.IndexOf(Of T)(array, search(0), index + 1) 'A value of -1 means we have searched to the end of the array and found nothing If index = -1 Then _ Return -1 match = True 'Assume we have a match until we discover that we don't. 'Loop through the search array from the second item to the last For searchIndex As Integer = 1 To search.Length - 1 'If an item doesn't then abort the for loop and continue searching array for matches If Not (array(searchIndex + index).Equals(search(searchIndex))) Then match = False Exit For End If Next End While 'If this code has been reached we have found a match. Return the index. Return index End Function 'This overload provides a default index of 0 (we could also use an optional argument) Public Function SearchArray(Of T)(ByVal array As T(), ByVal search As T()) As Integer Return SearchArray(Of T)(array, search, 0) End Function Usage is as follows: 'Search char array "MONKEYS" for "KEY" Dim Data As Char() = {"M"c, "O"c, "N"c, "K"c, "E"c, "Y"c, "S"c} Dim Search As Char() = {"K"c, "E"c, "Y"c} MessageBox.Show( _ SearchArray(Of Char)(Data, Search).ToString() _ ) Quote [sIGPIC]e[/sIGPIC]
Zeul1 Posted November 2, 2006 Author Posted November 2, 2006 Thanks i'll give it a try out. I wonder what sort of speed will be achieved compared to instrB? My arrays are normally upto 200,000 bytes What a shame .net doesn't have anything built in. Quote
Administrators PlausiblyDamp Posted November 2, 2006 Administrators Posted November 2, 2006 Out of interest what kind of information do your arrays store? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Zeul1 Posted November 2, 2006 Author Posted November 2, 2006 I am the author of DVDPlanner which is a DVD Authoring Solution, so i am dealing with elementry video and audio streams. From the video perspective, as the video file is piped through it is continually scanned for Header details which determines pretty much everything in the mux. Now with native VB InstrB has proved to be very quick, and i was hoping for further speed improvements with .net. Because .net now has intrinsic bit handling ie [<< and >>], these areas should be much faster than using lookup tables as i do now. If i can't match or beat the speed of InstrB, though, then the whole port to .net will not be worth it. I have already converted all of the API to remove the 'As Any' that is no longer supported, so to hit a hurdle now is irritating. Quote
Leaders snarfblam Posted November 2, 2006 Leaders Posted November 2, 2006 I didn't go to great lengths to optimize the code I wrote. If you are looking for raw speed you might want to write your own new function. Just because the function is written in VB doesn't mean that it will be slow. You can use a tool like .Net Reflector to examine the compiled code and do some benchmark tests to tweak the code. You could even write the code in MSIL if you want to get the most out of your CPU cycles. Quote [sIGPIC]e[/sIGPIC]
Zeul1 Posted November 3, 2006 Author Posted November 3, 2006 @marble_eater Many thanks for your advice Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.