Program to convert a character to ASCII code

357mag

Well-known member
Joined
Mar 31, 2023
Messages
58
Programming Experience
3-5
I'm trying to grab a character entered by the user and then converting it to ASCII number.

The output should look like this:


A is 65
B is 66
C is 67

But I need help putting it together. My code is wrong:
C#:
 private void buttonRun_Click(object sender, EventArgs e)
        {
            char ch = 0;
            int chValue = 0;
            string myString;

            ch = textBoxEntry.Text;
            myString = Convert.ToInt32(myString);
            

        }
 
ToInt32(String)Converts the specified string representation of a number to an equivalent 32-bit signed integer.
ToInt32(Char)Converts the value of the specified Unicode character to the equivalent 32-bit signed integer.
Line 7 is wrong because you can't assign a string to a char variable.
Line 8 is wrong because it would be using the first version that expect the string to represent a number, what you want is the second version and to do that you need to get a char from the string.
String Class (System) (Strings and indexes)
Here you see that you can index a string like an array and get a char, so at line 7 you can do .Text[0] to get the first char in string. Then on line 8 you need to use that ch as argument to convert.
 
I put a focus line of code when I click the Clear button and if I keep the focus the program runs fine. It will say like A is 65, B is 66 etc.
But if I comment out the focus line it does this:

A is 65
is 32
is 32

I don't understand why it would do that.
C#:
private void buttonRun_Click(object sender, EventArgs e)
        {
            char ch;
            int chValue = 0;

            ch = textBoxEntry.Text[0];
            chValue = Convert.ToInt32(ch);

            labelAnswer.Text = labelAnswer.Text + ch + " is " + chValue + "\r\n";
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxEntry.Text = " ";
           // textBoxEntry.Focus();
        }
 

Attachments

  • Visual Studio Screenshot.jpg
    Visual Studio Screenshot.jpg
    353.8 KB · Views: 13
Why would you set the Text of a TextBox to a space when you are supposed to be clearing it? The TextBox has a Clear method. Why not call that? If you're going to set the Text, at least set it to an empty string. 32 is the numeric value for the space character. Why is it surprising that you get that when that's what you're putting in the control?
 
"" is an empty string that has no characters, " " is a string that contains one space character. But as said, use the Clear method if you want to clear the textbox.
 
Get the first char, cast it to an int and tostring the int:

C#:
        private void buttonRun_Click(object sender, EventArgs e)
        {
            txtAnswer.Text = ((int)txtInput.Text[0]).ToString();
        }
 
Hello this is Gulshan Negi
Well, I searched about it on the Google and In my opinion you should try this code:
C#:
private void buttonRun_Click(object sender, EventArgs e)
{
    char ch = '\0';
    int chValue = 0;
    string myString;

    // Retrieve the character entered by the user
    myString = textBoxEntry.Text;

    // Check that the input is not empty
    if (!string.IsNullOrEmpty(myString))
    {
        // Retrieve the first character of the input
        ch = myString[0];

        // Convert the character to its ASCII value
        chValue = (int)ch;

        // Display the result
        MessageBox.Show(ch + " is " + chValue);
    }
    else
    {
        MessageBox.Show("Please enter a character.");
    }
}
Hope it will help you.
Thanks
 
Last edited by a moderator:
Back
Top Bottom