Link to home
Start Free TrialLog in
Avatar of kyrenator
kyrenator

asked on

Letters to numbers C#

How can i make a program in c# that it will take from a textbox a word e.x. "George" and it will make it a number like this : G=7, e=5, o=15, r=18, g=7, e=5 then the number of the word George is G+e+o+r+g+e = 7+5+15+18+7+5 = 57????
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

I reckon you want to use alphabet indeces as numbers for letter, am I right?

Really it is quite easy implement, e.g. by putting all the alphabet into a list ot char array.

Do you need some sample code?
Avatar of kyrenator
kyrenator

ASKER

yes if it is easy for you!
See code snippet
 

string inputField = "George";
 
System.Text.Encoding ascii = System.Text.Encoding.ASCII; 
Byte[] encodedBytes = ascii.GetBytes(inputField); 
 
// Initialize AsciiSum Field
int AsciiSum = 0;
 
foreach (Byte b in encodedBytes) 
{
	if ((b>64) && (b<91))
	{
		// The Letter A is ASCII 65 and the Letter Z is ASCII 90
		// subtract 64 to get the value we need to add to our ascii sum
		AsciiSum += b-64;
	}
	else
	{
		if ((b>96) && (b<123))
		{
			// The Letter a is ASCII 97 and the Letter z is ASCII 122
			// subtract 96 to get the value we need to add to our ascii sum
			AsciiSum += b-96;
		}
	}
}
MessageBox.Show(AsciiSum.ToString());

Open in new window

and how can i connect this with a textbox?
Try my code, it 's complete.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace charstonumber
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Dictionary<char, int> char2numDictionary = new Dictionary<char, int>();
        private void button1_Click(object sender, EventArgs e)
        {
            // convert string to lower case and convert to array of characters
            char[] chars2convert = textBox1.Text.ToLower().ToCharArray();
            // our result - initialize it to 0
            int result = 0;
            // loop- through chars:
            foreach (char ch in chars2convert)
            {
                int charInt = char2numDictionary[ch];
                result = result + charInt;
            }
            this.lblResult.Text = "" + result;
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // initialize dictionary with characters and corresponding numbers
            char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
 
            for (int i = 0; i < chars.Length; i++)
            {
                // char is key, number is value
                char2numDictionary.Add(chars[i], i + 1); // indfexes strts from 0 - so add 1
            }
        }
    }
}

Open in new window

Yeah, just forgot:

you have a TextBox1 at the form, the label calleslblResult, and a button called button 1.

If you need you may populate the dictionary with characters and related ascii numbers. I used alphabet index as a number (same as in your question).
Erron in line 7:

'WindowsFormsApplication7.Form1.Dispose(bool)': no suitable method found to override      

i am going to sleep now so i will see the other answers tomorrow! goodnight
ASKER CERTIFIED SOLUTION
Avatar of Jammer59
Jammer59
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
how can i make the button to check the radiobutton1 after it shows the AsciiSum to the label?
kyrenator - I am not sure I understand you question.    I don't know why you would have a radiobutton on your form.
AsciiSum should be visible in a label on your main form.  The label is named label2.  
I will include a screenshot of my output.  

asciiconvert.bmp
ok thats fine but I need a radiobutton to continue my program! how i can tell the button to check a radiobutton after finding the sum?
Can you provide a screenshot of your form with the radio button?  
I still do not understand the need for a radiobutton.  Were you provided with specifications on how the system needs to work.  If so, can you include them in your next message?
 
kyrenator, my solution is tested and working. And provides the output you mrequested!
What error are you talking about?

To check a radiobutton add the following code to the Button1 handler - I just display result on a messagebox:

            if (radioButton1.Checked){
                MessageBox.Show("checked");
            }
            else{
                MessageBox.Show("not checked");
            }
        private void button1_Click(object sender, EventArgs e)
        {
            // convert string to lower case and convert to array of characters
            char[] chars2convert = textBox1.Text.ToLower().ToCharArray();
            // our result - initialize it to 0
            int result = 0;
            // loop- through chars:
            foreach (char ch in chars2convert)
            {
                int charInt = char2numDictionary[ch];
                result = result + charInt;
            }
            this.lblResult.Text = "" + result;
 
            if (radioButton1.Checked){
                MessageBox.Show("checked");
            }
            else{
                MessageBox.Show("not checked");
            }
        }

Open in new window

yes but how can make the button after doing all this things then to check to radio button its own!
Your question is not quite clear -WHAT radiobuttons are you talking about? There wereno a word about radiobuttons in the original question. What type of check do you need? Explain the scenario pls.
ok leave i will find it!