Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help converting java-script to C#.

Help converting java-script to C#.

Scheduled Pinned Locked Moved C#
csharpjavajavascripttoolshelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    E3pO
    wrote on last edited by
    #1

    Hello! i'm a bit baffled when it comes to converting this javascript over to C#... Any help would be appreciated! Thank you :) Here is the javascript:

    function d(strInput) {
    strInput = decoder(strInput);
    var strOutput = "";
    var intOffset = (key + 112) / 12;
    for (i = 4; i < strInput.length; i++) {
    thisCharCode = strInput.charCodeAt(i);
    newCharCode = thisCharCode - intOffset;
    strOutput += String.fromCharCode(newCharCode)
    }
    document.write(strOutput)
    }

    And this is what i attempted at converting to C#.

     public string decode(int key, string data)
        {
            int i;
            string strInput = base64Decode(data);
            string strOutput = "";
            int intOffset = (key + 112) / 12;
            for (i = 4; i < strInput.Length; i++)
            {
                 char thisLetter = strInput\[i\];
    
                char thisCharCode = strInput\[i\];
              int newCharCode = thisCharCode - intOffset;
        
               strOutput += new String(new char\[\]{Convert.ToChar(newCharCode)});
    
            }
            return strOutput;
        }
    
    A E 2 Replies Last reply
    0
    • E E3pO

      Hello! i'm a bit baffled when it comes to converting this javascript over to C#... Any help would be appreciated! Thank you :) Here is the javascript:

      function d(strInput) {
      strInput = decoder(strInput);
      var strOutput = "";
      var intOffset = (key + 112) / 12;
      for (i = 4; i < strInput.length; i++) {
      thisCharCode = strInput.charCodeAt(i);
      newCharCode = thisCharCode - intOffset;
      strOutput += String.fromCharCode(newCharCode)
      }
      document.write(strOutput)
      }

      And this is what i attempted at converting to C#.

       public string decode(int key, string data)
          {
              int i;
              string strInput = base64Decode(data);
              string strOutput = "";
              int intOffset = (key + 112) / 12;
              for (i = 4; i < strInput.Length; i++)
              {
                   char thisLetter = strInput\[i\];
      
                  char thisCharCode = strInput\[i\];
                int newCharCode = thisCharCode - intOffset;
          
                 strOutput += new String(new char\[\]{Convert.ToChar(newCharCode)});
      
              }
              return strOutput;
          }
      
      A Offline
      A Offline
      AhsanS
      wrote on last edited by
      #2

      Firstly user StringBuilder and use append method for adding strings. It will improve performance. Second where is char thisLetter used? Third what is the error you are getting? Fourth why use Convert.ToChar when it already a character. Ahsan Ullah Senior Software Engineer MCTS 2.0

      E 1 Reply Last reply
      0
      • A AhsanS

        Firstly user StringBuilder and use append method for adding strings. It will improve performance. Second where is char thisLetter used? Third what is the error you are getting? Fourth why use Convert.ToChar when it already a character. Ahsan Ullah Senior Software Engineer MCTS 2.0

        E Offline
        E Offline
        E3pO
        wrote on last edited by
        #3

        Well, i got it to work only when the key is a negative number.. For some reason it's still not able to convert when data is a positive number. Example: (int key = 212, string data = "U0lra36DfImFkImOkImCW4OKj4h8hIdJfoqI") Output = {c¬a¬¬¬¬¬¬¬¬@¬¬¬¬a¬¬.c¬¬} Example2: (int key = -88, string data = "T1RXYmV0cHFkZ3R1MzQ1Ng==") Output = {crnobers1234} Code:

        public string decode(int key, string data)
        {
        int i;
        string strInput = base64Decode(data);
        StringBuilder strOutput = new StringBuilder("");

                int intOffset = (key + 112) / 12;
                for (i = 4; i < strInput.Length; i++)
                {
        
        	int thisCharCode = strInput\[i\];
            char newCharCode = (char)(thisCharCode - intOffset);
            strOutput.Append(newCharCode);
                }
                return strOutput.ToString();
            }
        
        1 Reply Last reply
        0
        • E E3pO

          Hello! i'm a bit baffled when it comes to converting this javascript over to C#... Any help would be appreciated! Thank you :) Here is the javascript:

          function d(strInput) {
          strInput = decoder(strInput);
          var strOutput = "";
          var intOffset = (key + 112) / 12;
          for (i = 4; i < strInput.length; i++) {
          thisCharCode = strInput.charCodeAt(i);
          newCharCode = thisCharCode - intOffset;
          strOutput += String.fromCharCode(newCharCode)
          }
          document.write(strOutput)
          }

          And this is what i attempted at converting to C#.

           public string decode(int key, string data)
              {
                  int i;
                  string strInput = base64Decode(data);
                  string strOutput = "";
                  int intOffset = (key + 112) / 12;
                  for (i = 4; i < strInput.Length; i++)
                  {
                       char thisLetter = strInput\[i\];
          
                      char thisCharCode = strInput\[i\];
                    int newCharCode = thisCharCode - intOffset;
              
                     strOutput += new String(new char\[\]{Convert.ToChar(newCharCode)});
          
                  }
                  return strOutput;
              }
          
          E Offline
          E Offline
          E3pO
          wrote on last edited by
          #4

          Solved: <pre>public string decoder(string data) { string b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; char o1, o2, o3; int h1, h2, h3, h4, bits, i = 0; string enc = ""; do { h1 = b64.IndexOf(data.Substring(i++, 1)); h2 = b64.IndexOf(data.Substring(i++, 1)); h3 = b64.IndexOf(data.Substring(i++, 1)); h4 = b64.IndexOf(data.Substring(i++, 1)); bits = h1 << 18 | h2 << 12 | h3 << 6 | h4; o1 = (char)(bits >> 16 & 0xff); o2 = (char)(bits >> 8 & 0xff); o3 = (char)(bits & 0xff); if (h3 == 64) enc += new string(new char[] { o1 }); else if (h4 == 64) enc += new string(new char[] { o1, o2 }); else enc += new string(new char[] { o1, o2, o3 }); } while (i < data.Length); return enc; } public string d(int key, string data) { int i; string strInput = decoder(data); StringBuilder strOutput = new StringBuilder(""); int intOffset = (key + 112) / 12; for (i = 4; i < strInput.Length; i++) { int thisCharCode = strInput[i]; char newCharCode = (char)(thisCharCode - intOffset); strOutput.Append(newCharCode); } return strOutput.ToString(); } </pre>

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups