Avatar of TheRookie32
TheRookie32

asked on 

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

Avatar of undefined
Last Comment
Bob Learned
SOLUTION
Avatar of razorback041
razorback041
Flag of United States of America image

Blurred text
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.
See Pricing Options
Start Free Trial
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of TheRookie32
TheRookie32

ASKER

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. :$
Avatar of TheRookie32
TheRookie32

ASKER

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...  
Avatar of razorback041
razorback041
Flag of United States of America image

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
Avatar of razorback041
razorback041
Flag of United States of America image

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

Avatar of TheRookie32
TheRookie32

ASKER

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...  :?
ASKER CERTIFIED SOLUTION
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.
Avatar of TheRookie32
TheRookie32

ASKER

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....
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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...   =)
Avatar of TheRookie32
TheRookie32

ASKER

@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...
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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

Bob
Avatar of TheRookie32
TheRookie32

ASKER

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

@LearnedOne - it deosnt get represented with a single digit, it gets represented as 26...
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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

Bob
Visual Basic.NET
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
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo