Results 1 to 3 of 3

Thread: [RESOLVED] convert this string to this string (Vague huh!?)

Threaded View

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Resolved [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:
    1. Private Function GetMsiNameFromGuid(ByVal GuidName As String) As String
    2.         Dim MsiNameParts() As String = GuidName.Replace("{", "").Replace("}", "").Split("-"c)
    3.         Dim MsiName As New StringBuilder
    4.  
    5.         'Just reverse the first 3 parts
    6.         For i As Integer = 0 To 2
    7.             MsiName.Append(ReverseString(MsiNameParts(i)))
    8.         Next
    9.  
    10.         'For the last 2 parts, reverse each character pair
    11.         For j As Integer = 3 To 4
    12.             For i As Integer = 0 To MsiNameParts(j).Length - 1
    13.                 MsiName.Append(MsiNameParts(j)(i + 1))
    14.                 MsiName.Append(MsiNameParts(j)(i))
    15.                 i += 1
    16.             Next
    17.         Next
    18.  
    19.         Return MsiName.ToString
    20. End Function
    21.  
    22. Private Function ReverseString(ByVal input As String) As String
    23.         Dim Chars() As Char = input.ToCharArray
    24.         Array.Reverse(Chars)
    25.         Return Chars
    26. 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)
    Last edited by chris128; Jan 12th, 2010 at 03:46 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width