Results 1 to 3 of 3

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

  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


  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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:
    1. Public Function GetMsiNameFromGuid(ByVal guid As Guid) As String
    2.     Dim guidParts As String() = guid.ToString().Split("-"c)
    3.     Dim concat As IEnumerable(Of Char) = guidParts(3).AsEnumerable().Concat(guidParts(4))
    4.     Dim msiName As New StringBuilder(32)
    5.  
    6.     For index As Integer = 0 To 2
    7.         msiName.Append(New String(guidParts(index).Reverse().ToArray()))
    8.     Next
    9.  
    10.     For index As Integer = 0 To concat.Count() - 1 Step 2
    11.         msiName.Append(concat(index + 1) & concat(index))
    12.     Next
    13.  
    14.     Return msiName.ToString()
    15.  
    16. End Function

    vb.net Code:
    1. Me.GetMsiNameFromGuid(New Guid("{01C5A10F-AD9B-405B-853A-6659841A1242}"))

  3. #3

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

    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.
    Last edited by chris128; Jan 12th, 2010 at 05:45 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