Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts

    useing javascript in asp

    This is a great little javascript I found that converts ascii to binary and vise versa what I was wondering is how I could change it so that when you submit info on a page it does the conversion and submits the binary value? instead so that the new page that loads shows the binary value or vise versa

    here is the script that I found

    Code:
    <html><head><title>Convert Binary to ASCII or ASCII to Binary</title>
    
    function dobin(text,sepa) {
    var letbin = ""
    for (i=0; i<text.length;i++) {
    
    let = text.substr(i,1);
    
    if (i>0) {
    var sep = sepa;
    if (i % 10 == 0) {
    	letbin=letbin+'<BR>'
    }
    }
    else {
    var sep = "";
    }
    
    //Ascii -- Binary Code
    
    //Numbers:
    if (let ==  "0") {letbin = letbin + sep + '00110000'}
    if (let ==  "1") {letbin = letbin + sep + '00110001'}
    if (let ==  "2") {letbin = letbin + sep + '00110010'}
    if (let ==  "3") {letbin = letbin + sep + '00110011'}
    if (let ==  "4") {letbin = letbin + sep + '00110100'}
    if (let ==  "5") {letbin = letbin + sep + '00110101'}
    if (let ==  "6") {letbin = letbin + sep + '00110110'}
    if (let ==  "7") {letbin = letbin + sep + '00110111'}
    if (let ==  "8") {letbin = letbin + sep + '00111000'}
    if (let ==  "9") {letbin = letbin + sep + '00111001'}
    
    }
    binary.innerHTML = letbin
    return false;
    }
    
    function doasc(text) {
    
    //if (text.length % 8 != 0) {
    //	alert (text + " is not an even binary.\n\nYou may have missed a digit or maybe added an additional digit/character.\n\nSeparators are NOT required here.")
    //	return false;
    //	last;
    //}
    
    var letasc = ""
    lettot = text.length / 8
    j=0
    for (i=0; i<lettot;i++) {
    
    let = text.substr(j,8);
    
    //Numbers:
    if (let ==  "00110000") {letasc = letasc + '0'}
    if (let ==  "00110001") {letasc = letasc + '1'}
    if (let ==  "00110010") {letasc = letasc + '2'}
    if (let ==  "00110011") {letasc = letasc + '3'}
    if (let ==  "00110100") {letasc = letasc + '4'}
    if (let ==  "00110101") {letasc = letasc + '5'}
    if (let ==  "00110110") {letasc = letasc + '6'}
    if (let ==  "00110111") {letasc = letasc + '7'}
    if (let ==  "00111000") {letasc = letasc + '8'}
    if (let ==  "00111001") {letasc = letasc + '9'}
    if (letasc == "") {
    	alert ("not found")
    	break;
    }
    j=j+8
    }
    ascii.innerHTML = letasc
    asciia.innerHTML = text.length
    return false;
    }
    </head>
    
    <body>
    
    <form name="asc2bin">
    	<strong>Convert to Binary</strong>
    	<br />
    	Binary Code gets wordwrapped if longer then 10 digits! so use notepad if trying to conver back to remove space
    	<br />
    	Enter Text: <input type="Text" name="text" size="92">
    	<br />
    	Separate with: <input type="Text" name="separater" maxlength="1" size="1"> (optional)<br>
    	<input type="Submit" value="Convert" onclick="dobin(document.asc2bin.text.value,document.asc2bin.separater.value);return false"><br>
    </form>
    
    <div name="binary" id="binary">Binary Conversion will show here.</div>
    
    <br>
    
    <form name="bin2asc">
    	<strong>Convert to Ascii</strong>
    	<br />
    	Enter Binary: <input type="Text" name="text" size="90">
    	<br />
    	<input type="Submit" value="Convert" onclick="doasc(document.bin2asc.text.value);return false"><br>
    </form>
    
    <div name="ascii" id="ascii">ASCII Conversion will show here.</div>
    <div name="asciia" id="asciia">ASCII Length will show here.</div>
    
    </body>
    </html>
    This is the code I would use instead of course doesnt work?

    Code:
    <html><head><title>Convert Binary to ASCII or ASCII to Binary</title>
    </head>
    
    <%
    '************************************ MAIN PROGRAM
    
    Select Case CInt(Request.QueryString("dbshow"))
    	Case 1
    		Thanks()
    	Case Else
    		Input()
    End Select
    
    '******************************** End MAIN PROGRAM
    %>
    
    <%Sub Input() '''''''''''''''''''''''''''''''''''%>
    <body>
    <script language="JavaScript" src="/Scripts/Ascii2Bin.js"></script>
    <form name="asc2bin" action="testpage.asp?dbshow=1" method="post">
    	<strong>Convert to Binary</strong>
    	<br />
    	Binary Code gets wordwrapped if longer then 10 digits! so use notepad if trying to conver back to remove space
    	<br />
    	Enter Text: <input type="Text" name="text" size="92">
    	<br />
    	Separate with: <input type="Text" name="separater" maxlength="1" size="1"> (optional)<br>
    	<input type="Submit" value="Convert" onclick="dobin(document.asc2bin.text.value)"><br>
    </form>
    
    <br>
    
    <form name="bin2asc">
    	<strong>Convert to Ascii</strong>
    	<br />
    	Enter Binary: <input type="Text" name="text" size="90">
    	<br />
    	<input type="Submit" value="Convert" onclick="doasc(document.bin2asc.text.value);return false"><br>
    </form>
    
    <div name="ascii" id="ascii">ASCII Conversion will show here.</div>
    <div name="asciia" id="asciia">ASCII Length will show here.</div>
    <%End Sub '''''''''''''''''''''''''''''''''''''''''''%>
    
    <%Sub Thanks() '''''''''''''''''''''''''''''''''''%>
    <%		bin	=	Request.Form("text")%>
    <input type="Text" name="texta" size="92" value="<%=bin%>">
    <%
    Response.Write "<SCRIPT LANGUAGE=""JavaScript"">" & vbCrlf
    Response.Write "dobin(document.asc2bin.texta.value);" & vbCrlf
    Response.Write "</SCRIPT>"
    %>
    <body>
    <%
    
    response.write(bin)
    
    End Sub '''''''''''''''''''''''''''''''''''''''''''%>
    </body>
    </html>
    useing the js to hold all of the script info

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Could you not just put some hidden fields in the form to carry the converted values, and make the functions submit the form (ie: validation) instead of changing the innerHTML?

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am not sure how to change the innerHTML part?? in code that is?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •