1

I'm trying to convert a string to ASCII and I added one button of convert to ASCII and 2 text boxes:

example of what should I need to get: for input text@gg.com I need to get: 116 101 120 116 064 103 103 046 099 111 109 for some reason I always get 78-74-40-67-67-2E-63-6F-6D

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ascii
{
  public partial class WebForm1 : System.Web.UI.Page
  {

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (char c in TextBox1.Text)
        {
             TextBox3.Text = Encoding.ASCII.GetString(new byte[] { });
        }
    }

    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
    }

  }
}

Thank you a lot for the help!

user3385217
  • 73
  • 1
  • 2
  • 8
  • 5
    Strings are already represented using Unicode, which is a superset of ASCII. What exactly are you trying to achieve? Do you want to display your characters as a sequence of ASCII codes? – Douglas Apr 09 '16 at 15:56
  • 1
    int.Parse will fail unless TextBox3.Text contains a valid number. You'll need to explain further what exactly you are trying to do though – Stuart Whitehouse Apr 09 '16 at 15:58
  • You can only convert the characters of a string to ascii int. Did you mean to do: `int.Parse(c)`? – sthomps Apr 09 '16 at 15:59
  • 1
    What should the output look like? – IS4 Apr 09 '16 at 16:01
  • You _really_ need to show an example of what you are trying to do here. Are you parsing numbers into characters? Like `65 66 67 => ABC` ? – John Alexiou Apr 09 '16 at 16:02
  • I need to get string in textbox1 and convert it to ASCII(int) in Textbox3. @ja72 – user3385217 Apr 09 '16 at 16:05
  • A string can contain multiple characters, each of which will have its own ASCII code. What do you want to show in textbox3 if there are multiple characters in the string? – dotNET Apr 09 '16 at 16:07
  • @dotNET for example for input text@gg.com I need to get: 116 101 120 116 064 103 103 046 099 111 109 – user3385217 Apr 09 '16 at 16:11
  • @user3385217 please edit this information into the question so that's it's clear what you want. – ChrisF Apr 09 '16 at 16:14
  • Possible duplicate of [How to get ASCII value of string in C#](http://stackoverflow.com/questions/400733/how-to-get-ascii-value-of-string-in-c-sharp) – Kevin Avignon Apr 09 '16 at 22:37

2 Answers2

2

Assuming that your strings consist only of ASCII characters, you can use:

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox3.Text = string.Join(" ", TextBox1.Text.Select(c => (int)c));
}

If your strings consist of non-ASCII characters, then their UTF-16 code units will be returned. If this is not desirable, you should include a check:

    if (TextBox1.Text.Any(c => c > 127))
         TextBox3.Text = "Invalid string";
Douglas
  • 53,759
  • 13
  • 140
  • 188
0

Assuming you want to convert the characters to ASCII and display their codes in decimal:

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox3.Text = String.Join(" ", Encoding.ASCII.GetBytes(TextBox1.Text));
}

Please note that this code first converts the text to ASCII, which covers only characters ranging from 0 to 127, so it depends on what you mean by "ASCII". If you want just Unicode number representations of the code points, use Douglas' answer.

IS4
  • 11,945
  • 2
  • 47
  • 86