# Number Substitution Generator

So i am trying to develop a real simple app that will take letters entered in a textbox and convert it to numbers in another.  (a or A = 1, b or B = 2).

How would i go about doing this?  I dont need the code (yet) but rather ideas on how it would be done and what methods etc to research in order to learn how to do it.

Thanks!
Visual Basic.NET

Last Comment
Bob Learned
SOLUTION
razorback041

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Carl Tawn

If you numbers are in sequence then you could just subtract a value from the characters ASCII representation.
I would use a HashTable so that you can associate any number, string, or combination of them with each of your letters regardless of whether the codes have a pattern or not.
TheRookie32

Ok I'll research the above...  I just wanted to do it for fun to generate a 'code' of sorts.  So the word hello in the first textbox would translate to 8-5-11-11-15.

And actually i may need to be adding a dash in between each letter(number) and leave the space between the words so the person trying to read it can know where one letter(number) begins and not have the word hello be: 85111115. :\$
TheRookie32

hmmm... so i dont quite have it.

I need to get some code in there to handle spaces as well.  (which is what the commented if statement was trying to do)

Heres the code for the button push:

Private Sub btnEncode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncode.Click
'Get the string of letters
Dim letters As String = txtCodeInput.Text
'trim any leading or trailing spaces
'letters.Trim()
'Determine the length of the string
Dim letterCount As Integer = txtCodeInput.TextLength
'set all the letters to lowercase as in my enums, they are all lowercase
letters.ToLower()

Dim sb As New System.Text.StringBuilder
Dim messageBefore As String
Dim messageAfter As Integer

Dim MyEnumVal As LetterNumber
Dim i As Integer

For i = 0 To letterCount

'If i = " " Then
'sb.Append(" ")
'Else
MyEnumVal = CType(i, LetterNumber)
sb.Append(MyEnumVal)
sb.Append("-")
'End If

Next

txtOutput.Text = sb.ToString

End Sub

'my abbreviated enum:

Public Enum LetterNumber
'Using this to subsitute the letters for numbers
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
End Enum

But alls that it is doing is printing the enums in order.. so if i enter cat into the top textbox, I get 0-1-2-3 in the bottom one...
razorback041

its in your loop...you are doing

for i = 0 to lettercount

and then you do

MyEnumVal = CType(i, LetterNumber)
sb.Append(MyEnumVal)

you are getting the number for i, not your letter

try

MyEnumVal = CType(letters(i), LetterNumber)

hth
razorback041

nm...your letters isnt an array...oops...i just assumed

not pretty..but

MyEnumVal = CType(letters.substring(i, 1), LetterNumber)

this isnt tested as i dont have vs open..but i think you can get the picture of what im doing

TheRookie32

gives me an error: 'Cast from string "a" to type 'Integer' is not valid.'

What i need to do is loop through each character in my string and replace that value with the value in my enums list but i am not achieving that goal yet...  :?
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
TheRookie32

Hey thanks for your post... I am not understanding how the letter is getting changed to a number though as when i step through the code, even when i am at the line of code:
'
txtOutput.Text = sb.ToString.Replace("- ", " ").Trim("-")
'
It appears that the value letter is still "a" and not 1.  Yet it posts 1 to the output message box.

As soon as it gets to end sub, the value of the letter changes to 1 but it seems that this change would happen earlier. (?)

So it works, just not sure why it does...
The "letter" variable will still have the value of the last letter in the string to be converted because of this line:

letter = letters.Substring(i, 1)

I assure you that the values inside the StringBuilder itself are not letters, but are in fact numbers, dashes and spaces.

How are you viewing what is inside the StringBuilder?  I can't figure it out...but I don't use the debugger often either....
Bob Learned

Look ma, no Hashtable:

Public Shared Function ConvertAlpha(ByVal input As String) As String

Dim output As New System.Text.StringBuilder(input.ToUpper())
For index As Integer = 1 To 26
output.Replace(Chr(index + 64), index)
Next

Return output.ToString()

End Function

Test:
Dim test As String = "This is a test of the radio broadcast system"
Dim conversion As String = ConvertAlpha(test)

Bob
No HashTable!  That's preposterous!  How can you have any pudding if you don't eat your meat!?...

I chose a HashTable (see my previous comment) "so that you can associate any number, string, or combination of them with each of your letters regardless of whether the codes have a pattern or not."

There are many solutions to this problem...   =)
TheRookie32

@Idle Mind - I trust this statement "I assure you that the values inside the StringBuilder itself are not letters, but are in fact numbers, dashes and spaces." as the code works!  ;)  But i just get confused that the letter variable doesnt change to a 1.  The output textbox seems to be magically populated with the numbers...

I cant view exactly what is in the stringbuilder, I was just watching the 'letter' variable in the watches window...

@TheLearnedOne - i cant get your code to work...  it returns quite a few numbers instead of 1 number for every letter...
The "letter" variable WON'T ever change to a number.

It is there to simply hold the current letter of the string that we are converting:

letter = letters.Substring(i, 1)

The line above pulls each letter indivually from the string in the loop.

It is this line that converts the letter to a number:

sb.Append(codes.Item(letter) & "-")

The "codes.Item(letter)" part returns the number equivalent of the value currently in "letter".  The value of letter is NOT changed though!  The return is immediately concatenated with a dash "-" and stored directly into the StringBuilder "sb".

I hope that makes sense...
Bob Learned

You have 26 letters, so how do you want to represent Z with a single digit?

Bob
TheRookie32

Ok thanks Idle Mind, i get it now... (sorry)  :\$

@LearnedOne - it deosnt get represented with a single digit, it gets represented as 26...
Bob Learned

1) ~IM's code does the same thing, so what do you want to happen

2) What should the result be for:

"The quick brown fox jumped over the lazy dog"

Bob
Hey Bob,

It's buried in the comments a couple posts down.  He wants to see it with dashes "-" in between each number:

"So the word hello in the first textbox would translate to 8-5-11-11-15."

So...

Public Shared Function ConvertAlpha(ByVal input As String) As String
Dim output As New System.Text.StringBuilder(input.ToUpper())
For index As Integer = 1 To 26
output.Replace(Chr(index + 64), index & "-")
Next
output.Replace("- ", " ")
Return output.ToString().Trim("-")
End Function
Bob Learned

Thanks, Mike, that WAS buried deep in the rain forest of Brazil ;)

Bob
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts

TRUSTED BY