Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Jan 2004
    Location
    Georgia
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Short C code to VB code - Simple & Urgent

    I am trying to change a short bit of C code to VB... here is the C code:
    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:
    Code:
    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
    Last edited by Megagix; 03-05-2004 at 10:27 PM.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    x /= 2; is short for x = x / 2;
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Registered User
    Join Date
    Jan 2004
    Location
    Georgia
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what would i substitute 'short' for in the line x = ((short)( key * -1.2456 ) + 1 ) * 65533?

    my VB code is like this so far:
    Code:
    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

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you're supposed to use CType for type casting in VB:

    Try:
    Code:
    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,
    Sadiq.

  • #5
    Registered User
    Join Date
    Jan 2004
    Location
    Georgia
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I dont believe that function is available in VB 6

  • #6
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    An Integer in VB is only 16 bits, so it would be the equivalent of a short in C. And the equivalent of:
    Code:
    (short)(some_expression);
    in VB is:
    Code:
    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

    Edit:
    Integer=16 bits not 16 bytes.
    Last edited by shmoove; 03-09-2004 at 05:30 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •