tsilenzio Posted March 26, 2009 Share Posted March 26, 2009 I am trying to setup two text boxes, one called Letters and one called Code If you type any word into the letters box, it should take out anything besides lower case letters, and numbers. And takes each character and converts it to ACSII, and seperates them by a period(.) And the Code text box is suppose to take it from that format and put it back into a string However when I try to get it to work it fails miserably and i've been trying to get it to work and am finding out I know alot less about javascript then I thought I did.. I still wont give up though! anyway here is my code function stringToCode(x) { var n = prepareString(document.getElementById(x).value); var code = ""; for(var i = 0; i < n.length; i++) { code += n.charCodeAt(i) + "."; } return code.substr(0, code.length - 2); } function codeToString(x) { var n = prepareString(document.getElementById(x).value); var letters = "", num = ""; for(var i = 0; i < n.length; i++) { if(n.charAt(i) == ".") { letters += n.fromCharCode(num) num = ""; } else { num += n.charAt(i) } } } function prepareString(n) { var validChars = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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", "."); n = n.toString(); n = n.toLowerCase(); for(var i = 0; i < n.length; i++) { var currChar = n.charAt(i); var delChar = false; for(var j = 0; j < validChars.length; j++) { if(currChar != validChars[j]) { delChar = true; } else { delChar = false; } } if(delChar) { n = n.replace(currChar, ""); } } alert(n); return n; } Quote Link to comment Share on other sites More sharing options...
corbin Posted March 26, 2009 Share Posted March 26, 2009 Hrmmm, this is what I came up with. I commented anything I thought you might have a question about, but if you want to know what's going on or why I did something a certain way, I'll gladly tell you. I originally planned to go through the entire code, but I have to go now lol. <script language="javascript"> function charToEncode(x) { //var n = prepareString(document.getElementById(x).value); if(!x.value) return ""; var n = x.value; //calling prepareString in this function doesn't make sense.... //also, I don't think it should be the responsibility of this function to get the object from the DOM based on an input name. //I would pass the object into the function. var code = ""; for(var i = 0; i < n.length; i++) { code += n.charCodeAt(i) + "."; } return ret = code.substr(0, code.length - 1); } function encodeToChar(x) { var n = x.value.replace(/[^0-9\.]/g, ''); //The box shouldn't contain any of the things either, so change the textarea contents. if(x.value != n) { x.value = n; } //using the split() method of the String class, you can just tokenize the string on the '.' delimiter. var ns = n.split('.'); var str = ""; for(var i = 0; i < ns.length; ++i) { if(ns[i].length) str = str + String.fromCharCode(ns[i]); } return str; } function UpdateValueByFieldId(field, v) { //on a live website, I would use a shorter function name //Also, this function is pretty useless, but I like to keep listeners (onkeypress, so on) to functions //Guess it's some weird OCD thing of mine. document.getElementById(field).value = v; } </script> <textarea id="char" style="width: 300px; height: 100px;" onkeypress="UpdateValueByFieldId('number', charToEncode(this));">Hello this is a sample sentence.</textarea> <br /> <button onclick="charToEncode('char');">Convert to numeric representation</button> <br /> <br /> <textarea id="number" style="width: 300px; height: 100px;" onkeypress="UpdateValueByFieldId('char', encodeToChar(this));">72.101.108.108.111.32.116.104.105.115.32.105.115.32.97.32.115.97.109.112.108.101.32.115.101.110.116.101.110.99.101.46</textarea> <br /> <button onclick="encodeToChar(document.getElementById('number'));">Convert to textual representation</button> <br /> Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted March 26, 2009 Author Share Posted March 26, 2009 I was wondering why u used a vairable name "ret" on this line: return ret = code.substr(0, code.length - 1); Quote Link to comment Share on other sites More sharing options...
corbin Posted March 26, 2009 Share Posted March 26, 2009 Oh x.x. It was an accident. I originally had: ret = ..... alert(ret); return ret; And I forgot to change it back to just returning. Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted March 26, 2009 Author Share Posted March 26, 2009 Okay, besides that everything worked fine BUT lol I had to change it from: onkeypress to: onkeyup because it was one value behinde on anything keyed in Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted March 26, 2009 Author Share Posted March 26, 2009 Forgot to mention, Thank you so much!!! Quote Link to comment Share on other sites More sharing options...
corbin Posted March 26, 2009 Share Posted March 26, 2009 Hrmmmm I would have thoguht onkeypress would have worked..... Oh well ;p. And no problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.