简体   繁体   中英

how to sort strings in vb

I am stuck with this problem. I am using Visual Studio Express 2013 (is that VB.NET?) and this is the task I would like to perform: I have a .txt file formatted like this (many more lines):

b2 a9 9c b4 d2 d3 52 02 da d2 e2 a2 c2 34 b2 a4 25 1c cb 52
00 00 00 00 00 00 00 00 00 00 04 c6 a2 10 a2 aa 5a 96 12 35 00 00 00 00 00 00 00 00 00 00 04 ae a5 9b 53 6c 15 56 56 d2 a1 54 55 b4 a6 ba a8 aa a6 b9 a8 44 94 69 5e d1 17 6a 56 9a 0b a7 29 49
69 d2 14 11 a1 78 41 d0 a4 54 41 51 1c 94 c1 24 a8 2a 71 14 50 14 04 b5 45 31 00 00 00 00 00 00 00 00 00 00

They are HEX values, I need to find repeating patterns. The pattern can be 2, 3, 4 or 5 bytes long, but for the moment it's ok to do it only with fixed size (ie only 2 bytes pattern). I want to populate a SortedDictionary with (Hex pattern, Repetition) I tried SortedDictionary and Dictionary, same issue. I have tested it for 10 lines or so step-by-step F11 debug and it works ok. However if I run the application with a 120 lines text file (which is small compared to what I want to do), it just hangs. The maximum number of elements in the dictionary would be 256*256, is it too much? What then with 3,4 or 5 bytes? It's not only a matter of time, the debugger raises an exception because the process doesn't end in 60 seconds. Is there a more clever way of doing what I want to do?

    Dim BytesList As New SortedDictionary(Of String, Integer)
    Dim line As String = ""
    Dim CurrentString As String
    Dim ByteLen As Byte

    For ByteLen = 2 To 2 'to do: ideally repeat for ByteLen = 2,3,4,5
        Using sr As StreamReader = New StreamReader(path & LOGFILENAME,False)
            Do
                line = line & sr.ReadLine()
                line = line.Replace(Chr(32), "") 'remove unwanted chars from line
                line = line.Replace(Chr(10), "")
                line = line.Replace(Chr(13), "")
                While (line.Length > ByteLen * 2)
                    CurrentString = line.Substring(0, ByteLen * 2)
                    line = line.Substring(2, line.Length - 2)
                    Try
                        BytesList.Add(CurrentString, 1)     'insert found address
                    Catch
                        BytesList(CurrentString) = BytesList(CurrentString) + 1 ' if string is already present, increment value
                    End Try
                End While
            Loop Until line Is Nothing
        End Using
    Next ByteLen
End Sub

Thanks in advance to everyone who helps!

If you intend to find repeated single bytes (hex-digit pairs) such as 00 00 00 00 then something like this would work:

Dim pattern As String = "(?<bh>\d\d )(\k<bh>)+"
Dim rx As New Regex(pattern)
Dim match = rx.Match("00 00 00 00 00 00 00 00 00 00 04 c6 a2 10 a2 aa 5a 96 12 35 00 00 00 00 00 00 00 00 00 00 04 ae a5 9b 53 6c 15 56 56 d2 a1 54 55 b4 a6 ba a8 aa a6 b9 a8 44 94 69 5e d1 17 6a 56 9a 0b a7 29 49 ")
While match.Success
    Debug.WriteLine("'{0}' found at position {1}", match.Value, match.Index)
    match = match.NextMatch()
End While

results

'00 00 00 00 00 00 00 00 00 00 ' found at 0
'00 00 00 00 00 00 00 00 00 00 ' found at 60
'56 56 ' found at 111

That's only for a single line (your 2nd line of your sample input), but you can easily expand that to work over each of the lines from the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM