char conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Q. John Chen

    #1

    char conversion

    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(encode d)
    {
    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;
    }
    }

  • Dietmar Meier

    #2
    Re: char conversion

    Q. John Chen wrote:
    [color=blue]
    > function cnvtBack(encode d)
    > {
    > 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;
    > }
    > }[/color]

    function cnvtBack(encode d) {
    var code, orig = "";
    for (i=0; i < encoded.length; i++) {
    var code = letter.charCode At(i);
    orig += String.fromChar Code(
    (code < 124 && code > 97)? --code : code
    );
    }
    return orig;
    }

    or

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

    ciao, dhgm

    Comment

    • McKirahan

      #3
      Re: char conversion

      "Q. John Chen" <qjchen@email.c om> wrote in message
      news:1106903493 .078440.161760@ f14g2000cwb.goo glegroups.com.. .[color=blue]
      > 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(encode d)
      > {
      > 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;
      > }
      > }
      >[/color]

      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>EnDecode r.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.fromChar Code(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('En code')">
      <input type="button" value="Decode" onclick="Do('De code')">
      <input type="reset" value="Reset">
      </form>
      </body>
      </html>


      Comment

      • Dr John Stockton

        #4
        Re: char conversion

        JRS: In article <1106903493.078 440.161760@f14g 2000cwb.googleg roups.com>
        , dated Fri, 28 Jan 2005 01:11:33, seen in news:comp.lang. javascript, Q.
        John Chen <qjchen@email.c om> posted :[color=blue]
        >
        >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.[/color]


        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 = "abcdefghijklmn opqrstuvwxyz" ; 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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        Working...