CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2013
    Posts
    2

    understanding some code

    Hello,

    i need some help on understanding some code. It's code to generate a password from an 8 digit input.
    i think it should be fairly easy for someone with a bit of experience.
    you have to excuse me if this isn't visual basic .net code, i think it is, but i'm not completely sure.

    Code:
      internal class CodeDecode
      {
        private int mivInputCode;
        private int mivOutputCode;
    
        public bool CheckInputCode(string psvInputCode)
        {
          return psvInputCode.Length == 8 && int.TryParse(psvInputCode, out this.mivInputCode) && this.mivInputCode >= 0;
        }
    
        public string CalculateOutputCode()
        {
          int[] numArray1 = new int[8];
          int[] numArray2 = new int[8];
          for (int index = 0; index < 8; ++index)
            numArray1[index] = this.mivInputCode % this.Power(10, index + 1);
          numArray2[0] = numArray1[0];
          for (int pivNumber2 = 1; pivNumber2 < 8; ++pivNumber2)
            numArray2[pivNumber2] = (numArray1[pivNumber2] - numArray1[pivNumber2 - 1]) / this.Power(10, pivNumber2);
          this.mivOutputCode = 1 + numArray2[1] + this.Power(3, numArray2[2]) + numArray2[3] + this.Power(5, numArray2[4]) + this.Power(4, numArray2[5]) + this.Power(7, numArray2[6]) + this.Power(2, numArray2[7]);
          return this.mivOutputCode.ToString();
        }
    
        private int Power(int pivNumber1, int pivNumber2)
        {
          int num = pivNumber1;
          if (pivNumber2 == 0)
            num = 1;
          if (pivNumber2 == 1)
            num = pivNumber1;
          if (pivNumber2 > 1)
          {
            for (int index = 1; index < pivNumber2; ++index)
              num *= pivNumber1;
          }
          return num;
        }
    
        public void WriteToFile(string psvFileName)
        {
          SaveFileDialog saveFileDialog = new SaveFileDialog();
          saveFileDialog.Filter = "Text files (*.txt)|*.txt";
          saveFileDialog.FileName = psvFileName;
          int num = (int) saveFileDialog.ShowDialog();
          psvFileName = saveFileDialog.FileName;
          using (StreamWriter text = File.CreateText(psvFileName))
          {
            text.WriteLine((string) (object) DateTime.Now + (object) text.NewLine);
            text.WriteLine("Input Code : {0}  (D1000)", (object) this.mivInputCode);
            text.WriteLine("Output Code: {0}", (object) this.mivOutputCode);
            text.Close();
          }
        }
      }
    if anyone could translate this for me, i would be very thankful.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: understanding some code

    [ moved to c# forum ]

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: understanding some code

    What part of the code don't you understand?

  4. #4
    Join Date
    Jul 2013
    Posts
    2

    Re: understanding some code

    i'm completely new at programming so there is almost nothing i understand about it.
    basically i only understand the last part, which is to save the result to a textfile.

    this is the part that i want to have translated to a human language
    Code:
        public string CalculateOutputCode()
        {
          int[] numArray1 = new int[8];
          int[] numArray2 = new int[8];
          for (int index = 0; index < 8; ++index)
            numArray1[index] = this.mivInputCode % this.Power(10, index + 1);
          numArray2[0] = numArray1[0];
          for (int pivNumber2 = 1; pivNumber2 < 8; ++pivNumber2)
            numArray2[pivNumber2] = (numArray1[pivNumber2] - numArray1[pivNumber2 - 1]) / this.Power(10, pivNumber2);
          this.mivOutputCode = 1 + numArray2[1] + this.Power(3, numArray2[2]) + numArray2[3] + this.Power(5, numArray2[4]) + this.Power(4, numArray2[5]) + this.Power(7, numArray2[6]) + this.Power(2, numArray2[7]);
          return this.mivOutputCode.ToString();
        }
    
        private int Power(int pivNumber1, int pivNumber2)
        {
          int num = pivNumber1;
          if (pivNumber2 == 0)
            num = 1;
          if (pivNumber2 == 1)
            num = pivNumber1;
          if (pivNumber2 > 1)
          {
            for (int index = 1; index < pivNumber2; ++index)
              num *= pivNumber1;
          }
          return num;
    i just want to know how the output is calculated. for example, when the input is 00636853 the result is 10796.

    my goal is to make an app for my smartphone that can calculate this code so i don't have to open my laptop everytime just to generate a code when i have to log on to one of our machines.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: understanding some code

    Please post the complete class and I'll give it a shot. I'm asking for the complete class because the code snippets you've included are missing the class constructor and I don't see anywhere the private Power() method is being called. So I need the entire class code to try to figure out what's going on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured