char conversion

Discussion in 'Javascript' started by Q. John Chen, Jan 28, 2005.

  1. Q. John Chen

    Q. John Chen Guest

    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;
    }
    }
     
    Q. John Chen, Jan 28, 2005
    #1
    1. Advertising

  2. 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
     
    Dietmar Meier, Jan 28, 2005
    #2
    1. Advertising

  3. Q. John Chen

    McKirahan Guest

    "Q. John Chen" <> wrote in message
    news:...
    > 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>
     
    McKirahan, Jan 28, 2005
    #3
  4. JRS: In article <>
    , dated Fri, 28 Jan 2005 01:11:33, seen in news:comp.lang.javascript, Q.
    John Chen <> 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.
     
    Dr John Stockton, Jan 29, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    597
  2. Tim Johansson
    Replies:
    15
    Views:
    8,403
    Christopher Benson-Manica
    Apr 8, 2004
  3. spoilsport
    Replies:
    5
    Views:
    3,865
    BartenderSam
    Mar 4, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,159
    Ian Collins
    May 9, 2006
  5. Perro Flaco

    const char* to char* conversion

    Perro Flaco, Jun 19, 2006, in forum: C++
    Replies:
    34
    Views:
    1,346
    Victor Bazarov
    Jun 21, 2006
Loading...

Share This Page