Sorry for the rubbish title but its a bit hard to explain in one line. Here's the basic premise though: I need to convert a GUID-like string to a different format as shown in these examples:
Code:Original String: {291B3A3B-F808-45B8-8113-DF232FCB6C82} Converted String B3A3B192808F8B541831FD32F2BCC628I originally thought the two strings were not even related but on closer inspection I can see that if you split the original string up into 2 halfs, in the first half each section (a section being delimited by the hyphen) is just reversed and in the second section characters are reversed in groups of 2. Its probably easier to see what I mean if I display the strings like this:Code:Original String: {01C5A10F-AD9B-405B-853A-6659841A1242} Converted String F01A5C10B9DAB50458A3669548A12124
So I know what I need to do, I'm just not sure of the best way to do this programmatically. This is what I have so far:Code:Original String: 01C5A10F AD9B 405B 85 3A 66 59 84 1A 12 42 Converted String F01A5C10 B9DA B504 58 A3 66 95 48 A1 21 24
vb.net Code:
Private Function GetMsiNameFromGuid(ByVal GuidName As String) As String Dim MsiNameParts() As String = GuidName.Replace("{", "").Replace("}", "").Split("-"c) Dim MsiName As New StringBuilder 'Just reverse the first 3 parts For i As Integer = 0 To 2 MsiName.Append(ReverseString(MsiNameParts(i))) Next 'For the last 2 parts, reverse each character pair For j As Integer = 3 To 4 For i As Integer = 0 To MsiNameParts(j).Length - 1 MsiName.Append(MsiNameParts(j)(i + 1)) MsiName.Append(MsiNameParts(j)(i)) i += 1 Next Next Return MsiName.ToString End Function Private Function ReverseString(ByVal input As String) As String Dim Chars() As Char = input.ToCharArray Array.Reverse(Chars) Return Chars End Function
It works, but it doesnt seem very well written... Oh and if you are wondering why I wrote my own ReverseString method rather than just using the Reverse extension method on the String class - It seems to return a collection of chars rather than a string (god knows why) and I couldnt find any easy/obvious way to convert this collection of chars back to a string (I thought just CStr would do it but apparently not)





Reply With Quote