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 / C++ / MFC
  4. Translating the Mod Operator in VB to c++ check

Translating the Mod Operator in VB to c++ check

Scheduled Pinned Locked Moved C / C++ / MFC
c++
6 Posts 3 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.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    I just want to get a check to see if my translation of the Mod Operator in VB to c++ is even close to right, or if there is a c++ equivalent to Mod, where I can just use Mod or something close to it. edit: I'm not sure if the null terminate char will produce a different result in the c++. So I have these lines of code to translate.

    strTextChar = Mid(strDomainName, (i Mod Len(strDomainName)) + 1, 1)
    strKeyChar = Mid(ApplicationName, (i Mod Len(ApplicationName)) + 1, 1)

    //vb Mod Operator = a - (b * (a / b))
    szTextChar = &szDomainName[ ( i - ( wcslen(szDomainName)) * (i / wcslen(szDomainName)) ) + 1 ];
    szKeyChar = &pzApplicationName[ ( i - ( wcslen(szApplicationName)) * (i / wcslen(szApplicationName)) ) + 1 ];

    I have this other line of code to translate, this one is more complex, just thought I would throw it out there. I took a peek at Xor, it's not quite sinking in yet.

    strTextChar = Chr(Asc(strTextChar) Xor intTemp)

    P J D 3 Replies Last reply
    0
    • J jkirkerx

      I just want to get a check to see if my translation of the Mod Operator in VB to c++ is even close to right, or if there is a c++ equivalent to Mod, where I can just use Mod or something close to it. edit: I'm not sure if the null terminate char will produce a different result in the c++. So I have these lines of code to translate.

      strTextChar = Mid(strDomainName, (i Mod Len(strDomainName)) + 1, 1)
      strKeyChar = Mid(ApplicationName, (i Mod Len(ApplicationName)) + 1, 1)

      //vb Mod Operator = a - (b * (a / b))
      szTextChar = &szDomainName[ ( i - ( wcslen(szDomainName)) * (i / wcslen(szDomainName)) ) + 1 ];
      szKeyChar = &pzApplicationName[ ( i - ( wcslen(szApplicationName)) * (i / wcslen(szApplicationName)) ) + 1 ];

      I have this other line of code to translate, this one is more complex, just thought I would throw it out there. I took a peek at Xor, it's not quite sinking in yet.

      strTextChar = Chr(Asc(strTextChar) Xor intTemp)

      P Offline
      P Offline
      Philippe Mori
      wrote on last edited by
      #2

      % is the modulo operator in C++ and C# and most similar languages.

      Philippe Mori

      J 1 Reply Last reply
      0
      • P Philippe Mori

        % is the modulo operator in C++ and C# and most similar languages.

        Philippe Mori

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        Thanks Philippe! I took off the +1 for the null terminator

        szTextChar = szDomainName[ (i % wcslen(szDomainName)) ];
        szKeyChar = pzApplicationName[ (i % wcslen(szDomainName)) ];

        1 Reply Last reply
        0
        • J jkirkerx

          I just want to get a check to see if my translation of the Mod Operator in VB to c++ is even close to right, or if there is a c++ equivalent to Mod, where I can just use Mod or something close to it. edit: I'm not sure if the null terminate char will produce a different result in the c++. So I have these lines of code to translate.

          strTextChar = Mid(strDomainName, (i Mod Len(strDomainName)) + 1, 1)
          strKeyChar = Mid(ApplicationName, (i Mod Len(ApplicationName)) + 1, 1)

          //vb Mod Operator = a - (b * (a / b))
          szTextChar = &szDomainName[ ( i - ( wcslen(szDomainName)) * (i / wcslen(szDomainName)) ) + 1 ];
          szKeyChar = &pzApplicationName[ ( i - ( wcslen(szApplicationName)) * (i / wcslen(szApplicationName)) ) + 1 ];

          I have this other line of code to translate, this one is more complex, just thought I would throw it out there. I took a peek at Xor, it's not quite sinking in yet.

          strTextChar = Chr(Asc(strTextChar) Xor intTemp)

          J Offline
          J Offline
          jkirkerx
          wrote on last edited by
          #4

          I'm trying to translate a program I use in vb to c++, to get the exact same results. The program is basically 8 lines of code. I have the first 2 lines working, but on line 3, I get back a int value of 10, and vb returns &h10, which is 16. So this is the vb loop of the function. I decided to use char since the valid characters are

          Private Const VALID_CHARACTERS = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
          Private Const DEFAULT_FORMAT = "&&&&-&&&&-&&&&-&&&&"

          This is the source code from vb I use.

          For i = 1 To CountAmpersands(sFormat)
          L1 strTextChar = Mid(strDomainName, (i Mod Len(strDomainName)) + 1, 1)
          L2 strKeyChar = Mid(ApplicationName, (i Mod Len(ApplicationName)) + 1, 1)
          L3 intTemp = (((Asc(strKeyChar) * i) * Len(ApplicationName) + 1) Mod Len(ValidCharacters) + 1) 'vb returns &H10 which is 16
          L4 strTextChar = Chr(Asc(strTextChar) Xor intTemp)
          L5 intTemp = (((Asc(strKeyChar) * i) * Len(DomainName) + 1) Mod Len(ValidCharacters) + 1)
          L6 strTextChar = Chr(Asc(strTextChar) Xor intTemp)
          L7 intEncryptedChar = ((Asc(strTextChar) Xor Asc(strKeyChar)) Mod Len(ValidCharacters)) + 1
          L8 strKey = strKey & Mid(ValidCharacters, intEncryptedChar, 1)

          Select Case i
          Case 4, 8, 12
          strKey = strKey & "-"
          End Select

          Next i

          And this is my feeble translation to c++, I didn't do very good on it, this is the 3rd run.

          for (int i = 1; i <= iCount; ++i) {
          L1 szTextCharA = szDomainNameA[ ( i % strlen( szDomainNameA )) ]; // Correct
          L2 szKeyCharA = szAppKeyA[ ( i % strlen( szAppKeyA )) ]; // Correct
          L3 iTemp = ((szKeyCharA * i) * strlen(szAppKeyA)) % strlen( LICENSEKEY_CHARACTERS );//iTemp 16 or &H10
          L4 szTextCharA = (char) ((int)szTextCharA ^ iTemp ); // iTemp should be 16 or &H10
          L5 iTemp = (int)(szKeyCharA * i) * strlen(szDomainNameA) % strlen( LICENSEKEY_CHARACTERS );
          L6 szTextCharA = (char)((int)szTextCharA ^ iTemp);
          L7 iEncryptedChar = (int)szTextCharA ^ (int)szKeyCharA % strlen(LICENSEKEY_CHARACTERS);

          // Build up the License Key till it's done.
          if ( i == 1) {
          strncpy_s( szKeyA, iKeyA, &LICENSEKEY_CHARACTERS[ iEncryptedChar ], 1 );
          }
          else {
          strncat_s( szKeyA, iKeyA, &LICENSEKEY_CHARACTERS[ iEncryptedChar ], 1 );
          }

          } // End of for loop

          Question1: On Line 3 in VB, I get back &H10, and Line 3 in c++, I get back 10. I'm not sure

          1 Reply Last reply
          0
          • J jkirkerx

            I just want to get a check to see if my translation of the Mod Operator in VB to c++ is even close to right, or if there is a c++ equivalent to Mod, where I can just use Mod or something close to it. edit: I'm not sure if the null terminate char will produce a different result in the c++. So I have these lines of code to translate.

            strTextChar = Mid(strDomainName, (i Mod Len(strDomainName)) + 1, 1)
            strKeyChar = Mid(ApplicationName, (i Mod Len(ApplicationName)) + 1, 1)

            //vb Mod Operator = a - (b * (a / b))
            szTextChar = &szDomainName[ ( i - ( wcslen(szDomainName)) * (i / wcslen(szDomainName)) ) + 1 ];
            szKeyChar = &pzApplicationName[ ( i - ( wcslen(szApplicationName)) * (i / wcslen(szApplicationName)) ) + 1 ];

            I have this other line of code to translate, this one is more complex, just thought I would throw it out there. I took a peek at Xor, it's not quite sinking in yet.

            strTextChar = Chr(Asc(strTextChar) Xor intTemp)

            D Offline
            D Offline
            Dave Doknjas
            wrote on last edited by
            #5

            The second one will be something like:

            strTextChar = static_cast(static_cast(strTextChar[0]) ^ intTemp);

            David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com

            J 1 Reply Last reply
            0
            • D Dave Doknjas

              The second one will be something like:

              strTextChar = static_cast(static_cast(strTextChar[0]) ^ intTemp);

              David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com

              J Offline
              J Offline
              jkirkerx
              wrote on last edited by
              #6

              Thanks Dave, Your hint gave me insight into the proper and more consistent way to write the 8 lines of code. Now I'm getting accurate results so far going through the loop. Looks like I will be able to complete this today and move on.

              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