PDA

View Full Version : Short C code to VB code - Simple & Urgent

Megagix
03-05-2004, 03:49 PM
I am trying to change a short bit of C code to VB... here is the C code:

unsigned short Password( unsigned long key )
{
short x;

x = ((short)( key * -1.2456 ) + 1 ) * 65533;
x = ( x / 2 + 7 ) * 3;
x /= 2;
return x * x;
}

THe code above is copied from a site and i dont have much experiance with C... the line i am getting stuck on is the 'x /=2'.. what does it do?

Here is VB my code so far:

Function Gps(ky As Long)
Dim X As Long
X = ky
X = X * -1.2456
X = X + 1
X = X * 65533
X = X / 2
X = X + 7
X = X * 3
'??????
Gps = X
End Function

I need my VB code to match up to the C code

I know my VB code is a little expanded and could easily be made simpler but I need the code to be precise. Could some one please help me!

Thank you,
-Jason

Edit: C code correction

liorean
03-05-2004, 03:52 PM
x /= 2; is short for x = x / 2;

Megagix
03-08-2004, 03:38 PM
what would i substitute 'short' for in the line x = ((short)( key * -1.2456 ) + 1 ) * 65533?

my VB code is like this so far:

Dim x As Integer
Dim short As Integer
x = ((short) * (Val(key) * -1.2456) + 1) * 65533
x = (x / 2 + 7) * 3
x = x / 2
GPS = x * x

THis may seem like the stupidest post ever, but I dont know anything about c programming.

Thank you,
-Jason

03-09-2004, 12:22 AM
I think you're supposed to use CType for type casting in VB:

Try:

x = ( CType( (Val(key) * -1.2456), Short ) + 1) * 65533

And see how that works out for you.

Also, you don't need the short variable, and x should be dimmed as Short, not Integer.

Hope that helps,

Megagix
03-09-2004, 03:34 PM
I dont believe that function is available in VB 6

shmoove
03-09-2004, 04:17 PM
An Integer in VB is only 16 bits, so it would be the equivalent of a short in C. And the equivalent of:

(short)(some_expression);

in VB is:

CInt(some_expression)

The main problem with converting this function to VB is all the overflow-protection VB gives you. In the C he's obviously overflowing the short variable a few times in the function, and in VB you'll get a runtime error for something like that, so you'll need to use a variable that's "big" enough and fake all the conversions.
It's been I while since I coded anything in C (back in the good-old 16-bit DOS days), but from what I can tell that C code will perform differently depending on what platform/environment/compiler is used. So to really make a VB version of this function you'll probably need someone with some real C experience to look at what the code is doing on a bit by bit basis, taking into consideration the unsigned nature of the key and the platform it was compiled on, and the resulting VB function might look quite different that it's C counterpart.
Like I said, it's been a while since I did any C, so anyone feel free to correct me if I'm wrong.

shmoove

Integer=16 bits not 16 bytes.