Convert VB Code

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
LVL 19
BrianGEFF719Asked:
Who is Participating?
 
deightonConnect With a Mentor Commented:
#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
 
deightonCommented:
that's c++ (probably c compatible)
0
 
BrianGEFF719Author Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
BrianGEFF719Author Commented:
attempted compilation with VC++ 6.

-Brian
0
 
BrianGEFF719Author Commented:
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
 
BrianGEFF719Author Commented:
excuse me i'm stupid...it work perfect! You are a very good programmer, thanks a lot...

-Brian
0
 
deightonCommented:
'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
 
BrianGEFF719Author Commented:
C:\Windows\Desktop\aui source\testtt\testtt.cpp(35) : error C2065: 'time' : undeclared identifier
0
 
deightonCommented:
did you include time.h?

What version of c++ are you using?
0
 
BrianGEFF719Author Commented:
yep thats the prob... #include <time.h>

Thanks for all your help!
0
 
BrianGEFF719Author Commented:
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
 
deightonCommented:
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
 
BrianGEFF719Author Commented:
Thanks for the response, I must say man...you are a really good programmer.
0
 
BrianGEFF719Author Commented:
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
 
deightonCommented:
make sure you have

double powx(double, long);


at the top section of the program
0
 
BrianGEFF719Author Commented:
k thanks for all your help, its all working now :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.