char conversion

Q

Q. John Chen

It should be easy but I am not familier with javascript.

I have a string that is encoded simply by replacing the letter with the
next letter in the alphabeta. I need a function to convert it back.
Following is the function:

function cnvtBack(encoded)
{
var orig = "";
for (i=0; i < encoded.length; i++)
{
var letter = "";
letter = encoded.charAt(i);
if (letter > 'a' && letter <= 'z')
letter = <<previous letter in alphabeta >> // HOW?
orig += letter;
}
}
 
D

Dietmar Meier

Q. John Chen said:
function cnvtBack(encoded)
{
var orig = "";
for (i=0; i < encoded.length; i++)
{
var letter = "";
letter = encoded.charAt(i);
if (letter > 'a' && letter <= 'z')
letter = <<previous letter in alphabeta >> // HOW?
orig += letter;
}
}

function cnvtBack(encoded) {
var code, orig = "";
for (i=0; i < encoded.length; i++) {
var code = letter.charCodeAt(i);
orig += String.fromCharCode(
(code < 124 && code > 97)? --code : code
);
}
return orig;
}

or

function cnvtBack(encoded) {
return encoded.replace(
/./g,
function(s) {
var code = s.charCodeAt(0);
return String.fromCharCode(
(code < 124 && code > 97)? --code : code
)
}
);
}

ciao, dhgm
 
M

McKirahan

Q. John Chen said:
It should be easy but I am not familier with javascript.

I have a string that is encoded simply by replacing the letter with the
next letter in the alphabeta. I need a function to convert it back.
Following is the function:

function cnvtBack(encoded)
{
var orig = "";
for (i=0; i < encoded.length; i++)
{
var letter = "";
letter = encoded.charAt(i);
if (letter > 'a' && letter <= 'z')
letter = <<previous letter in alphabeta >> // HOW?
orig += letter;
}
}

Here's a solution that both Encodes and Decodes a string.

Only letters are converted; all other characters remain the same.

Try it as=is; watch for word-wrap.

<html>
<head>
<title>EnDecoder.htm</title>
<script type="text/javascript">
function Do(what) {
var code = document.form1.Code.value;
if (code == "") return;
var temp = "";
for (i=0; i<code.length; i++) {
var char = code.charCodeAt(i);
var okay = false;
if (char >= 65 && char <= 90) okay = true;
if (char >= 97 && char <= 122) okay = true;
if (okay) {
if (what == "Decode") {
char--;
if (char == 64 || char == 96) char = char + 26;
}
if (what == "Encode") {
char++;
if (char == 91 || char == 123) char = char - 26;
}
}
temp += String.fromCharCode(char);
}
document.form1.Code.value = temp;
}
</script>
</head>
<body>
<form action="" method="get" name="form1">
<input type="text" name="Code" id="Code" value="">
<input type="button" value="Encode" onclick="Do('Encode')">
<input type="button" value="Decode" onclick="Do('Decode')">
<input type="reset" value="Reset">
</form>
</body>
</html>
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Fri, 28 Jan 2005 01:11:33, seen in Q.
John Chen said:
I have a string that is encoded simply by replacing the letter with the
next letter in the alphabeta. I need a function to convert it back.


Can the string contain upper case letters as well as lower case (and, if
so, should z be encoded to a or A?)? Can it contain characters that do
not get encoded?

Function Xfr will encode/decode for a shift of any number of positions;
to decode, change the sign of n.


K = "abcdefghijklmnopqrstuvwxyz" ; L = K.length
n = 1

function Xfr(msg, key, S) { var Z = "", i, Q, T
for (i=0; i < msg.length; i++) {
T = key.indexOf(Q = msg.charAt(i))
Z += T>=0 ? key.substr((T+S+L)%L, 1) : Q }
return Z }

MSG = "fred az"

Enc = Xfr(MSG, K, +n)
Dec = Xfr(Enc, K, -n)


It allows non-encoded characters. It should be fast enough. K can be
shuffled, to make a less trivial code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top