Solved

Convert VB Code

Posted on 2002-03-12
16
147 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
Comment Utility
#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
Comment Utility
that's c++ (probably c compatible)
0
 
LVL 19

Author Comment

by:BrianGEFF719
Comment Utility
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
Comment Utility
attempted compilation with VC++ 6.

-Brian
0
 
LVL 19

Author Comment

by:BrianGEFF719
Comment Utility
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
Comment Utility
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
Comment Utility
'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
Comment Utility
C:\Windows\Desktop\aui source\testtt\testtt.cpp(35) : error C2065: 'time' : undeclared identifier
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 18

Expert Comment

by:deighton
Comment Utility
did you include time.h?

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

Author Comment

by:BrianGEFF719
Comment Utility
yep thats the prob... #include <time.h>

Thanks for all your help!
0
 
LVL 19

Author Comment

by:BrianGEFF719
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the response, I must say man...you are a really good programmer.
0
 
LVL 19

Author Comment

by:BrianGEFF719
Comment Utility
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
Comment Utility
make sure you have

double powx(double, long);


at the top section of the program
0
 
LVL 19

Author Comment

by:BrianGEFF719
Comment Utility
k thanks for all your help, its all working now :)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

763 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

10 Experts available now in Live!

Get 1:1 Help Now