Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > char conversion

Reply
Thread Tools

char conversion

 
 
Q. John Chen
Guest
Posts: n/a
 
      01-28-2005
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;
}
}

 
Reply With Quote
 
 
 
 
Dietmar Meier
Guest
Posts: n/a
 
      01-28-2005
Q. John Chen wrote:

> 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
 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      01-28-2005
"Q. John Chen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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>


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      01-29-2005
JRS: In article <(E-Mail Removed) .com>
, dated Fri, 28 Jan 2005 01:11:33, seen in news:comp.lang.javascript, Q.
John Chen <(E-Mail Removed)> posted :
>
>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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments