Web Analytics Made Easy -
StatCounter Short C code to VB code - Simple & Urgent - CodingForum

Announcement

Collapse
No announcement yet.

Short C code to VB code - Simple & Urgent

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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; Mar 5, 2004, 04:27 PM.

  • #2
    x /= 2; is short for x = x / 2;
    liorean <[[email protected]]>
    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

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          I dont believe that function is available in VB 6

          Comment


          • #6
            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; Mar 9, 2004, 11:30 AM.

            Comment

            Working...
            X