[RESOLVED] convert this string to this string (Vague huh!?)
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
B3A3B192808F8B541831FD32F2BCC628
Code:
Original String:
{01C5A10F-AD9B-405B-853A-6659841A1242}
Converted String
F01A5C10B9DAB50458A3669548A12124
I 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 85 3A 66 59 84 1A 12 42
Converted String
F01A5C10 B9DA B504 58 A3 66 95 48 A1 21 24
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:
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)
Re: convert this string to this string (Vague huh!?)
This is what I got, same concept, I don't think there is any other "better" way:
vb.net Code:
Public Function GetMsiNameFromGuid(ByVal guid As Guid) As String
Dim guidParts As String() = guid.ToString().Split("-"c)
Dim concat As IEnumerable(Of Char) = guidParts(3).AsEnumerable().Concat(guidParts(4))
Dim msiName As New StringBuilder(32)
For index As Integer = 0 To 2
msiName.Append(New String(guidParts(index).Reverse().ToArray()))
Next
For index As Integer = 0 To concat.Count() - 1 Step 2
msiName.Append(concat(index + 1) & concat(index))
Next
Return msiName.ToString()
End Function
vb.net Code:
Me.GetMsiNameFromGuid(New Guid("{01C5A10F-AD9B-405B-853A-6659841A1242}"))
Re: convert this string to this string (Vague huh!?)
Thanks, as long as I wasnt overlooking something obvious and am not going about it a really strange way then I guess thats all I wanted to know really.
Cheers
Chris
EDIT: Oh and thanks for showing me how to use the String.Reverse method properly :) I dont understand why they didnt just make it return a String but oh well.
EDIT EDIT: D'oh! I've just realised that I was testing this in a new project which automatically targets .NET 3.5, where as my actual project that I want to use this in is .NET 2.0 so I cant use the Reverse extension method after all! Looks like I'll have to stick to my ReverseString method for now.