I've been working at this code for probably 7 hours since last night. After hours of troubleshooting I solved most issues with my code but now I have a strange problem that I cannot figure out how to fix.
One of my function is to generate a random hex string of 12 characters total. To do this I used a standard random string generator to generate a single character then convert that character over to hex, looped 6 times to create a total of 12 characters or 6 sets of hex
Here's the code:
Public Function HexGen2()
Dim Store As String = String.Empty 'This will be our final result. 6 sets of hex equalling 12 characters total
Dim cur As Integer = 0 'For looping the generator
Try
Do
Dim Strn As String = Nothing 'Randomly generated string
Dim ToHex As String = Nothing 'Hex value of above string
Dim rand As New Random() 'Initialize Random class
Dim Chrs() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray() 'Characters allowed to generate
Try
Strn += Chrs(rand.Next(Chrs.Length)) 'Generate
Catch ex As Exception
MsgBox("Error: " & ex.Message)
End Try
For i As Integer = 0 To Strn.Length - 1
ToHex &= Asc(Strn.Substring(i, 1)).ToString("x").ToUpper 'Convert Strn over to hex
Next
Store &= ToHex & "-" 'Storing hex result and appending a hyphen
'MsgBox(cur & vbNewLine & Store)
cur += 1 'Increment loop
Loop Until cur = 6 'Loop 6 times
Catch ex As Exception
MsgBox("Error: " & ex.Message)
End Try
Return Store.Remove(Store.Length - 1, 1) 'Get rid of last hyphen
End Function
My problem is when it's executed, it spits out 6 sets of hex, which are all the same. Oddly enough, if I use that msgbox I commented out, it works and gives me 6 seperate sets of hex but if I don't use that msgbox, I get all the same sets.
I don't know why that is happening and I don't know if it's because I'm burning myself out on this or what but anybody got a solution? I'd greatly appreciate it. Thanks!
I know the code is sloppy and there are probably better ways of generating 6 sets of hex but I don't know it.