Jump to content

[SOLVED] String to ASCII, And vice versa


tsilenzio

Recommended Posts

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;
}

Link to comment
Share on other sites

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 />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.