[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Convert VB Code

Posted on 2002-03-12
16
Medium Priority
?
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 6
16 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

656 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