2

I would like to know how to make this code work becouse currently it doesn't work. I should get string and it supposed to convert it to ASCII. I have 2 text boxes with input and the output result and one button the does the conversion. Big thanks for the help!!!

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Convert to ASCII</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        Enter your mail:
        <input TextBox ID="TextBox1" runat="server"     ontextchanged="TextBox1_TextChanged"><TextBox/>

</p>
        <Button type="Button" onclick="Ascii()">Convert to ASCII</button>

</p>
        Output:
        <input TextBox ID="TextBox2" runat="server"     ontextchanged="TextBox2_TextChanged"><TextBox/>
</p>
<p>

<script>
        function Ascii() {
        textbox2 = String.charCodeAt(TextBox1);
           }
         </script>
       </p>
    </form>
</body>
</html>
user3385217
  • 73
  • 1
  • 2
  • 8
  • You need to loop for the length of the string calling yourString.charCodeAt(index) for each character. – Alex K. Apr 16 '16 at 16:01

4 Answers4

3

Use charCodeAt() javascript function

Already mentioned it is javascript function

user3045179
  • 321
  • 4
  • 20
0

TextBox1 and TextBox2 are entire objects. Specify the values and loop over the characters

function Ascii() {
    var ascii = "";
    for (var i = 0, len = TextBox1.value.length; i < len; i++) {
      ascii = ascii + TextBox1.value.charCodeAt(i);
    }
    TextBox2.value = ascii;
}
Nath Papadacis
  • 155
  • 2
  • 9
0

You can use

  1. from character to ASCII

    charCodeAt();

  2. from ASCII to character:

    String.fromCharCode()

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
0

try to use following way...

 <div>
    Enter your mail:
    <input TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"><TextBox/>
 </div>
 <div>
 <Button type="Button" onclick="Ascii('TextBox1')">Convert to ASCII</button>
 </div>
 <div>
    Output:
    <input TextBox ID="TextBox2" runat="server"     ontextchanged="TextBox2_TextChanged"><TextBox/>
</div>

 <script type="text/javascript">
    function Ascii(TextBox1) 
    {
        var val = document.getElementById(TextBox1).value;
        alert(val.charCodeAt(0));
        document.getElementById('TextBox2').value = val.charCodeAt(0);
    }
   function StringToAscii(TextBox1) 
    {
        var val = document.getElementById(TextBox1).value;
        for(var i = 0; i < val.length; i++) {
            document.getElementById('TextBox2').value += val.charCodeAt(i);
        }
    }
</script>

so using .charCodeAt(0) you will get ascii value...

Devendra Gohel
  • 112
  • 1
  • 5