Results 1 to 12 of 12

Thread: [RESOLVED] Crazy conversion of unique integer to alphabetical string

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Resolved [RESOLVED] Crazy conversion of unique integer to alphabetical string

    Hey guys, got another crazy idea to throw at you

    I have a string that is comprised from two separate unique numbers (Only Numbers, that are random lengths)
    Code:
    Dim PrePhrase As String = DatabaseModule.UserID + DatabaseModule.ModelID
    This is then converted into a character array in which all numbers are separated into linear positions in the array
    Code:
    Dim charArr() As Char = PrePhrase.ToCharArray()
    I now want to be able to create an algorithm that states that each number in the array is converted from it's integer form (e.g. 121) to it's alphabetical form (e.g. onetwoone).

    Hope this makes sense and I appreciate any help

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: Crazy conversion of unique integer to alphabetical string

    Are you really willing to concatenate all the number names together like that? I would think that a space would make it much more readable. Still, this is a good situation to make use of a Dictionary(of Integer, String). You would set the dictionary up with the 10 digits and their corresponding written names. Next you would iterate through the digits and convert them to strings. The efficient way to do this would be before you converted the numbers into a string, but that would take more effort, since you would have to use integer Mod and integer division (\) to extract each digit in turn.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Re: Crazy conversion of unique integer to alphabetical string

    Thank you that sounds like a really good way of doing it. Have you got any sample code you could show (in vb.net) please?

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Crazy conversion of unique integer to alphabetical string

    vb.net Code:
    1. Public Class Form1
    2.     Dim d As New Dictionary(Of Char, String)
    3.  
    4.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    5.         Dim n() As String = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}
    6.         For i = 0 To 9
    7.             d.Add(CChar(i.ToString), n(i)) ' adds a key/value pair to the dictionary
    8.         Next
    9.         NumConvert("1232456789")
    10.     End Sub
    11.  
    12.     Private Sub NumConvert(s As String)
    13.         Dim sb As New System.Text.StringBuilder
    14.         For Each c In s
    15.             sb.Append(d.Item(c)) ' returns the value associated with the key in the dictionary
    16.         Next
    17.         Me.Text = sb.ToString
    18.     End Sub
    19. End Class
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Crazy conversion of unique integer to alphabetical string

    @dunfiddlin...
    sb.Append(d.Item(c)) ???

    not:
    sb.Append(d(c)) ???

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Crazy conversion of unique integer to alphabetical string

    Quote Originally Posted by .paul. View Post
    @dunfiddlin...
    sb.Append(d.Item(c)) ???

    not:
    sb.Append(d(c)) ???
    Damnation! Hoist upon mine own petard! Yup, 2nd version's better but both work. Hey, it's late. I can't be perfect all the time!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Re: Crazy conversion of unique integer to alphabetical string

    Thanks for the code. Before you replied though, I created a routine that I think may also work but I have a small issue. Here's the code:

    Code:
     
     'Adds UserID and ModelID together to make a unique number (Since they are added in string form this means that if for example, the userID = 10, modelID = 2, then the result is 102 which is a unique number)
    ' This also means that the result Phrase is variable length due to the increasing size of length of the ID's (e.g. UserID could be 1 or it could be 200000)
            Dim PrePhrase As String = DatabaseModule.UserID + GETCurrentModelID
            'Creates a character array, Prephrase digits are separated and passed into character array
            Dim charArr() As Char = PrePhrase.ToCharArray()
    
            Dim ChartoArray As Array = charArr
            Dim Phrase As String
    
            For i As Integer = 0 To ChartoArray.Length - 1
                If ChartoArray(i).Equals(1) Then
                    Phrase = Phrase & "one"
                    i = i + 1
                End If
                If ChartoArray(i).Equals(2) Then
                    Phrase = Phrase & "two"
                    i = i + 1
                End If
                If ChartoArray(i).Equals(3) Then
                    Phrase = Phrase & "three"
                    i = i + 1
                End If
                If ChartoArray(i).Equals(4) Then
                    Phrase = Phrase & "four"
                    i = i + 1
                End If
                If ChartoArray(i).Equals(5) Then
                    Phrase = Phrase & "five"
                    i = i + 1
                End If
                If ChartoArray(i).Equals(6) Then
                    Phrase = Phrase & "six"
                    i = i + 1
                End If
                If ChartoArray(i).Equals(7) Then
                    Phrase = Phrase & "seven"
                    i = i + 1
                End If
                If ChartoArray(i).Equals(8) Then
                    Phrase = Phrase & "eight"
                    i = i + 1
                End If
                If ChartoArray(i).Equals(9) Then
                    Phrase = Phrase & "nine"
                    i = i + 1
                End If
                If ChartoArray(i).Equals(0) Then
                    Phrase = Phrase & "zero"
                    i = i + 1
                End If
            Next
    The problem I am having is that the loop runs but it doesn't compare the number in the Array with the " .Equals(numbergoeshere) " bit. Also I'm not sure if " CharToArray(i) " i is actually giving the position in the array or not

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Crazy conversion of unique integer to alphabetical string

    try this:

    Code:
    Dim charArr() As Integer = Array.ConvertAll(PrePhrase.ToCharArray(), Function(c) CInt(c.ToString))
    Dim Phrase As String = ""
    
    For i As Integer = 0 To charArr.Length - 1
        If charArr(i).Equals(1) Then
            Phrase = Phrase & "one"
            i = i + 1
        End If
        If charArr(i).Equals(2) Then
            Phrase = Phrase & "two"
            i = i + 1
        End If
        If charArr(i).Equals(3) Then
            Phrase = Phrase & "three"
            i = i + 1
        End If
        If charArr(i).Equals(4) Then
            Phrase = Phrase & "four"
            i = i + 1
        End If
        If charArr(i).Equals(5) Then
            Phrase = Phrase & "five"
            i = i + 1
        End If
        If charArr(i).Equals(6) Then
            Phrase = Phrase & "six"
            i = i + 1
        End If
        If charArr(i).Equals(7) Then
            Phrase = Phrase & "seven"
            i = i + 1
        End If
        If charArr(i).Equals(8) Then
            Phrase = Phrase & "eight"
            i = i + 1
        End If
        If charArr(i).Equals(9) Then
            Phrase = Phrase & "nine"
            i = i + 1
        End If
        If charArr(i).Equals(0) Then
            Phrase = Phrase & "zero"
            i = i + 1
        End If
    Next

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Re: Crazy conversion of unique integer to alphabetical string

    Worked perfectly when I took "i = i + 1" out. Thank you!

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Crazy conversion of unique integer to alphabetical string

    From the documentation:
    "If you change the value of counter while inside a loop, your code might be more difficult to read and debug."

    The way this is coded is a mistake waiting to happen. Try it with a charArr equal to one item, and that one item is 1.

    dun's approach slightly modified:

    Code:
        Dim d As New Dictionary(Of Char, String)
    
        Private Function NumConvert(num As String) As String
            If d.Count <= 0 Then
                Dim n() As String = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}
                For i As Integer = 0 To 9
                    d.Add(CChar(i.ToString), n(i)) ' adds a key/value pair to the dictionary
                Next
            End If
            Dim sb As New System.Text.StringBuilder
            For Each c As Char In num
                sb.Append(d(c)) ' returns the value associated with the key in the dictionary
            Next
            Return sb.ToString
        End Function
    
        Private Function NumConvert(num As Integer) As String
            Return NumConvert(num.ToString)
        End Function
    Last edited by dbasnett; Mar 30th, 2013 at 09:56 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Crazy conversion of unique integer to alphabetical string

    here's another method in vb2012:

    Code:
    Dim d As New Dictionary(Of Integer, String) From {{0, "zero"}, {1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}, {6, "six"}, {7, "seven"}, {8, "eight"}, {9, "nine"}}
    Dim PrePhrase As String = "0123456789"
    Dim charArr() As Integer = Array.ConvertAll(PrePhrase.ToCharArray(), Function(c) CInt(c.ToString))
    Dim Phrase As String = String.Concat(Array.ConvertAll(charArr, Function(x) d(x)))

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    30

    Re: Crazy conversion of unique integer to alphabetical string

    Thank you, I will try that later, it defiantly looks like a more efficient way of doing it.

Tags for this Thread

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