Click to See Complete Forum and Search --> : Is there a way to break down a word into numbers?
goofy1989
08-18-2006, 04:07 AM
I wasn't too sure wich category this would go into, hopefully you can do this in Javascript (most likely) or ASP.
But here's what I want to do:
I wan to have a text input. When the user types a word in, the letters are broken down into numbers.
So for example, if the user types "DAD", then that would be converted into D=4, A=1, D=4, so 414=DAD.
Do you get what I'm saying?
MOM=131513
How can this be done? Thanks in advance!
Tweak4
08-18-2006, 08:50 AM
You would have to write a custom function to do it, which shouldn't be all that difficult. However, you would definitely have to keep in mind the need for leading zeros for A through J- DAD should equal 040104, instead of 414. Otherwise, when you have a code 131513, you'll never know if it should be MOM (13-15-13) or ACAEAC (1-3-1-5-1-3), or some other combination.
If you have some letters that get a one position numerical equivelant and some that get 2 positions, you'll end up with a big mess in the end.
goofy1989
08-18-2006, 10:49 AM
Thank you for replying. How could this be done? Is there a link for something like that? What would that be called?
PineSolPirate
08-18-2006, 10:59 AM
You could set up an array, a big one, or use this lovely ascii converter and just subtract 64 from the number.
http://sharkysoft.com/tutorials/jsa/content/018.html
Remember though, capitals and lowercase are different ASCII values, so you might want to run toLowerCase() or toUpperCase() first.
http://www.hscripts.com/tutorials/javascript/string-case-converting.php
felgall
08-18-2006, 04:21 PM
Fairly trivial exercise as Javascript actually has its own ASCII converter built in - so that script is just reinventing the roller when we already have wheels.
function toNum(words) {
var l = words.length;
var num = '';
for (var i=0; i < l; i++) {
v = words.charCodeAt(i)-64;
num += v < 10 ? '0'+v : String(v);
}
return num;
}
NogDog
08-18-2006, 05:08 PM
It would be similarly simple in PHP:
function str2num($string)
{
$result = '';
for($i=0; $i<strlen($string); $i++)
{
$result .= ord($string[$i]);
}
return($result);
}
...or using Perl...sub str2num {
my $result = '';
my $string = shift;
for (split '', $string) { $result .= ord $_; }
return $result;
}
ray326
08-18-2006, 10:34 PM
No one but Stephen seems to have taken Tweak's admonition to heart.
goofy1989
08-19-2006, 12:04 AM
I want to try what stephen wrote, but JavaScript really isn't my forte. Could somebody please post a whole page that could incorporate this. Possibly by having a text input and having that be what gets converted? Thanks in advance!
And also, I use mostly ASP, so is there some version of a way to do this in ASP?
felgall
08-19-2006, 12:39 AM
Here's a basic form that will do it with the function I provided earlier:
<form name="conv">
<input type="text" name="in"><br>
<input type="button" onclick="conv.out.value=toNum(conv.in.value)"><br>
<input type="text" name="out">
</form>
goofy1989
08-19-2006, 03:05 PM
Alright, so I got that working perfectly, thanks for that form as a guideline. Do you think that I could have the output not be in a form, but as plain text below the form, and have it update automatically, instead of having to press the button? That'd be cool.
Here's what I used to make it work. At first it didn't work because it didn't like the first input being named "in" so I changed it and it worked:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" language="javascript">
function toNum(words) {
var l = words.length;
var num = '';
for (var i=0; i < l; i++) {
v = words.charCodeAt(i)-64;
num += v < 10 ? '0'+v : String(v);
}
return num;
}
</script>
</head>
<body>
<form name="conv">
<table>
<tr>
<td align="right">Input:</td>
<td><input type="text" name="passw" value=""></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" value="Click Me!" onClick="conv.out.value=toNum(conv.passw.value)"></td>
</tr>
<tr>
<td align="right">Output:</td>
<td><input type="text" name="out"></td>
</tr>
</table>
</form>
</body>
</html>
And again, doing this in ASP, or getting the javascript function to work with an ASP application would be the way to go for me. Does anybody know how to do this? Thanks in advance!
EDIT:
Oh, and one last thing, I need another Javascript function to break it back down into it's original text.
felgall
08-19-2006, 03:52 PM
This will convert the output field from a form field:
<form name="conv">
<input type="text" name="in"><br>
<input type="button" onclick="document.getElementById('out').innerHTML=toNum(conv.in.value)">
</form>
<div id="out"></div>
To get rid of the button you'd need to replace the onclick of the button with an onkeypress on the input field and then update the content of the output rather than overwriting the whole thing. You'd also need to handle deletion of different characters which would make things a lot more complex.
If you want this in any language other than Javascript then it would have to reload the whole page for every change.
NogDog
08-19-2006, 11:12 PM
No one but Stephen seems to have taken Tweak's admonition to heart.
It wasn't part of the OP's requirement, which had specific examples not using leading zeros. By coding to the original requirements, I keep open the possibility of the "customer" realizing the error of his ways after code delivery, resulting in an emergency change request for which I can charge overtime rates.
;)
goofy1989
08-20-2006, 04:00 PM
Thans to all that have helped thus far, especially felgall, but it would still be helpful if we could decrypt it as well. Is this easily done? I think I thought of a way to make it work even without being decrypted, but it would be easier if that weren't the case.
Thanks in advance!
ray326
08-20-2006, 04:54 PM
By coding to the original requirements, I keep open the possibility of the "customer" realizing the error of his ways after code delivery, resulting in an emergency change request for which I can charge overtime rates....thereby increasing shareholder value. :D Well done!
felgall
08-20-2006, 05:04 PM
To decode it (if you added the leading zeros to the single digits) would just be a matter of splitting the string into two character pieces, converting those pieces to numbers, adding 64 to it and using String.fromCharCode() to convert it back into the original character.
function toChar(num) {
var st = num.split('');
var l = num.length;
var w = '';
for (var i = 0; i < l; i+=2) {
n = Number(st[i]+st[i+1]);
w+= String.fromCharCode(n);
}
return w;
}
If you didn't add the leading zeros then you wont know if 19 is S or AI.
goofy1989
08-27-2006, 03:55 AM
Thanks for all the replies, there is still one last thing that I need to make this complete:
I need a script that takes a form variable from the previous page (using ASP) and automatically converts it and can place it into another form encoded