Solved

Convert VB Code

Posted on 2002-03-12
16
153 Views
Last Modified: 2010-05-02
Hi,
I need a VB Function converted one of these languages (C, C++, Java or Perl)...I dont need it converted to all of them, just one of them...Thanks.


Public Function GenerateSerialNumber(FullName As String, UniqueID As String) As String
retry:
Randomize
randkey = Int(Rnd * 99 + 1)
If Len(randkey) < 2 Then
   GoTo retry:
End If
serial = randkey & "-"
For X1 = 1 To Len(FullName) Step 3
   j = Mid(FullName, X1, 1)
   p = p & Int(Asc(j) + randkey - 9 / 2 ^ Int(Mid(randkey, 1, 1)))
   If Len(p) > 6 Then
       Exit For
   End If
Next X1
For X2 = 1 To Len(UniqueID) Step 2
   jx = Mid(UniqueID, X2, 1)
   If IsNumeric(jx) = True Then
       jf = jf & Int(jx * randkey / 2 ^ Int(Mid(randkey, 1, 1)) / 3 * randkey)
   Else
       jf = jf & Int(randkey * Mid(Asc(jx), 1, 1) / 2 ^ Int(Mid(randkey, 1, 1)))
   End If
Next X2
serial = serial & p & "-" & jf
GenerateSerialNumber = serial
End Function
0
Comment
Question by:BrianGEFF719
  • 10
  • 6
16 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 200 total points
ID: 6860213
#include <stdio.h>
#include <stdlib.h>         /* For _MAX_PATH definition */
#include <string.h>
#include <math.h>

char * generateserialnumber(char *fullname, char *uniqueid, char * serial);


void main ()
{

     char serial[4096];

     
     generateserialnumber("deighton", "x3", serial);

     puts (serial);

     

}


char * generateserialnumber(char *fullname, char *uniqueid, char * serial)
{
     long randkey, x1,x2,lval;
     char p[4096];
     char sval[4096];
     char stemp[80];
     char jf[80];
     char j, jx;

     double dval;

     *p = 0;
     *sval = 0;
     *stemp = 0;
     *jf = 0;

     do{
          //randkey=rand() % 100;
          randkey = 62;
     } while (randkey < 10);

     
     sprintf(serial, "%li-", randkey);
     
     for(x1=1; x1 <= (signed long) strlen(fullname); x1+=3)
     {
          j = fullname[x1 - 1];

          dval =  9 / pow(2,int(randkey/10));

          lval = int((double) j + randkey - dval);
         
          sprintf(sval,"%li",lval);

          //puts(sval);
         
          strcat(p,sval);
         

          if (strlen(p) > 6)
               break;

     }



     for(x2=1; x2 <= (signed long) strlen(uniqueid); x2+=2)
     {
          jx = uniqueid[x2 - 1];

          if (jx >= '0' && jx <= '9')
          {

               dval = pow(2,randkey / 10);

               lval = int((double) (jx - '0') * randkey / dval /3 * randkey);

          }
          else
          {
               
               sprintf(stemp,"%i", (int) jx);
               dval = pow(2,randkey / 10);

               lval = int( (double) randkey * (stemp[0] - '0') / dval);                    

          }

          printf("%li\n",lval);              
         
          sprintf(sval,"%li",lval);
         
          strcat(jf,sval);


     }

     strcat(serial, p);
     strcat(serial, "-");
     strcat(serial, jf);
     

         
     return serial;



}

     
0
 
LVL 18

Expert Comment

by:deighton
ID: 6860214
that's c++ (probably c compatible)
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 6860232
C:\Windows\Desktop\aui source\serial\serial.cpp(13) : error C2065: 'generateserialnumber' : undeclared identifier
C:\Windows\Desktop\aui source\serial\serial.cpp(21) : error C2373: 'generateserialnumber' : redefinition; different type modifiers
C:\Windows\Desktop\aui source\serial\serial.cpp(44) : error C2065: 'strlen' : undeclared identifier
C:\Windows\Desktop\aui source\serial\serial.cpp(48) : error C2065: 'pow' : undeclared identifier
C:\Windows\Desktop\aui source\serial\serial.cpp(56) : error C2065: 'strcat' : undeclared identifier
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 6860234
attempted compilation with VC++ 6.

-Brian
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 6860250
i found out why i was getting those 5 errors, because i had

#include <stdafx.h>.

Now when i do it with just your code...i get the following compilation error

--------------------Configuration: ssss - Win32 Debug--------------------
Compiling...
sr.cpp
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/ssss.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

ssss.exe - 2 error(s), 0 warning(s)
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 6860291
excuse me i'm stupid...it work perfect! You are a very good programmer, thanks a lot...

-Brian
0
 
LVL 18

Expert Comment

by:deighton
ID: 6860299
'This is morte up to date, for debug reasons I had fixed randon nmber at 64 - this version randomizes like the VB version.


#include <stdio.h>
#include <stdlib.h>         /* For _MAX_PATH definition */
#include <string.h>
#include <math.h>
#include <time.h>

char * generateserialnumber(char *fullname, char *uniqueid, char * serial);


void main ()
{

     char serial[4096];

     
     generateserialnumber("deighton", "x3", serial);

     puts (serial);

     

}


char * generateserialnumber(char *fullname, char *uniqueid, char * serial)
{
     long randkey, x1,x2,lval;
     char p[4096];
     char sval[4096];
     char stemp[80];
     char jf[80];
     char j, jx;

     double dval;

     *p = 0;
     *sval = 0;
     *stemp = 0;
     *jf = 0;

     
     srand( (unsigned)time( NULL ) );



     do{
          randkey=rand() % 100;
          //randkey = 62;
     } while (randkey < 10);

     
     sprintf(serial, "%li-", randkey);
     
     for(x1=1; x1 <= (signed long) strlen(fullname); x1+=3)
     {
          j = fullname[x1 - 1];

          dval =  9 / pow(2,int(randkey/10));

          lval = int((double) j + randkey - dval);
         
          sprintf(sval,"%li",lval);

          //puts(sval);
         
          strcat(p,sval);
         

          if (strlen(p) > 6)
               break;

     }



     for(x2=1; x2 <= (signed long) strlen(uniqueid); x2+=2)
     {
          jx = uniqueid[x2 - 1];

          if (jx >= '0' && jx <= '9')
          {

               dval = pow(2,randkey / 10);

               lval = int((double) (jx - '0') * randkey / dval /3 * randkey);

          }
          else
          {
               
               sprintf(stemp,"%i", (int) jx);
               dval = pow(2,randkey / 10);

               lval = int( (double) randkey * (stemp[0] - '0') / dval);                    

          }

               
         
          sprintf(sval,"%li",lval);
         
          strcat(jf,sval);


     }

     strcat(serial, p);
     strcat(serial, "-");
     strcat(serial, jf);
     

         
     return serial;



}

     
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 6860353
C:\Windows\Desktop\aui source\testtt\testtt.cpp(35) : error C2065: 'time' : undeclared identifier
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Expert Comment

by:deighton
ID: 6860441
did you include time.h?

What version of c++ are you using?
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 6860468
yep thats the prob... #include <time.h>

Thanks for all your help!
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 6863454
i'm trying to compile this on a unix platform..I'm getting this error.

/tmp/ccOCAplh.o(.text+0x134): undefined reference to `pow'
/tmp/ccOCAplh.o(.text+0x292): undefined reference to `pow'
/tmp/ccOCAplh.o(.text+0x348): undefined reference to `pow'    

I made sure to include math.h, whats the problem??/
0
 
LVL 18

Expert Comment

by:deighton
ID: 6863877
I'm not sure if the pow function is 'standard or not' - there's several options

1) find some docs on the unix implementation.
2) go to the c++ section and post my code, see if anyone knows how to fix it

3) re-write the pow function.

here I've done that in a way which should meet the needs of your problem, rasing a double to a positive whole number.

#include <time.h>

char * generateserialnumber(char *fullname, char *uniqueid, char * serial);


double powx(double, long);

void main ()
{

     char serial[4096];

     
     generateserialnumber("deighton", "x3", serial);

     puts (serial);

     

}


char * generateserialnumber(char *fullname, char *uniqueid, char * serial)
{
     long randkey, x1,x2,lval;
     char p[4096];
     char sval[4096];
     char stemp[80];
     char jf[80];
     char j, jx;

     double dval;

     *p = 0;
     *sval = 0;
     *stemp = 0;
     *jf = 0;

     
     srand( (unsigned)time( NULL ) );



     do{
          randkey=rand() % 100;
          //randkey = 62;
     } while (randkey < 10);

     
     sprintf(serial, "%li-", randkey);
     
     for(x1=1; x1 <= (signed long) strlen(fullname); x1+=3)
     {
          j = fullname[x1 - 1];

          dval =  9 / powx(2,int(randkey/10));

          lval = int((double) j + randkey - dval);
         
          sprintf(sval,"%li",lval);

          //puts(sval);
         
          strcat(p,sval);
         

          if (strlen(p) > 6)
               break;

     }



     for(x2=1; x2 <= (signed long) strlen(uniqueid); x2+=2)
     {
          jx = uniqueid[x2 - 1];

          if (jx >= '0' && jx <= '9')
          {

               dval = powx(2,randkey / 10);

               lval = int((double) (jx - '0') * randkey / dval /3 * randkey);

          }
          else
          {
               
               sprintf(stemp,"%i", (int) jx);
               dval = powx(2,randkey / 10);

               lval = int( (double) randkey * (stemp[0] - '0') / dval);                    

          }

               
         
          sprintf(sval,"%li",lval);
         
          strcat(jf,sval);


     }

     strcat(serial, p);
     strcat(serial, "-");
     strcat(serial, jf);
     

         
     return serial;



}

     
double powx(double x, long y)
{


     long c;
     double d=1;
     for(c=0;c<y;c++)
          d *= x;

     return d;

}
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 6863890
Thanks for the response, I must say man...you are a really good programmer.
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 6866530
Quick question someone who is trying to compile this on a unix platform is getting this error:

"CustomAlgorithm.cpp: In function `char * generateserialnumber(char *,
char *, char *)':
CustomAlgorithm.cpp:43: implicit declaration of function `int powx(...)'
Exit 1"

do you have any suggestions?
0
 
LVL 18

Expert Comment

by:deighton
ID: 6876273
make sure you have

double powx(double, long);


at the top section of the program
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 6876294
k thanks for all your help, its all working now :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now