|
-
Mar 26th, 2013, 05:22 PM
#1
Thread Starter
Junior Member
[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
-
Mar 26th, 2013, 05:43 PM
#2
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
 
-
Mar 27th, 2013, 04:22 PM
#3
Thread Starter
Junior Member
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?
-
Mar 27th, 2013, 07:57 PM
#4
Re: Crazy conversion of unique integer to alphabetical string
vb.net Code:
Public Class Form1
Dim d As New Dictionary(Of Char, String)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim n() As String = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}
For i = 0 To 9
d.Add(CChar(i.ToString), n(i)) ' adds a key/value pair to the dictionary
Next
NumConvert("1232456789")
End Sub
Private Sub NumConvert(s As String)
Dim sb As New System.Text.StringBuilder
For Each c In s
sb.Append(d.Item(c)) ' returns the value associated with the key in the dictionary
Next
Me.Text = sb.ToString
End Sub
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!
-
Mar 27th, 2013, 08:07 PM
#5
Re: Crazy conversion of unique integer to alphabetical string
@dunfiddlin...
sb.Append(d.Item(c)) ???
not:
sb.Append(d(c)) ???
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 27th, 2013, 08:11 PM
#6
Re: Crazy conversion of unique integer to alphabetical string
 Originally Posted by .paul.
@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!
-
Mar 29th, 2013, 09:32 PM
#7
Thread Starter
Junior Member
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
-
Mar 29th, 2013, 09:53 PM
#8
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 30th, 2013, 06:34 AM
#9
Thread Starter
Junior Member
Re: Crazy conversion of unique integer to alphabetical string
Worked perfectly when I took "i = i + 1" out. Thank you!
-
Mar 30th, 2013, 09:45 AM
#10
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.
-
Mar 30th, 2013, 12:04 PM
#11
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)))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 31st, 2013, 07:25 PM
#12
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|