#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Help With This Code


    I need help with the following code, I'm not sure which parts of it Python supports. The following code is from Visual basic 6.

    Code:
    Private Sub InitCRC32()
        Dim i As Long, J As Long, K As Long, XorVal As Long
        
        Static CRC32Initialized As Boolean
        If CRC32Initialized Then Exit Sub
        CRC32Initialized = True
        
        For i = 0 To 255
            K = i
            
            For J = 1 To 8
                If K And 1 Then XorVal = CRC32_POLYNOMIAL Else XorVal = 0
                If K < 0 Then K = ((K And &H7FFFFFFF) \ 2) Or &H40000000 Else K = K \ 2
                K = K Xor XorVal
            Next
            
            CRC32Table(i) = K
        Next
    End Sub
    
    Private Function CRC32(ByVal Data As String) As Long
        Dim i As Long, J As Long
        
        Call InitCRC32
        
        CRC32 = &HFFFFFFFF
        
        For i = 1 To Len(Data)
            J = CByte(Asc(Mid(Data, i, 1))) Xor (CRC32 And &HFF&)
            If CRC32 < 0 Then CRC32 = ((CRC32 And &H7FFFFFFF) \ &H100&) Or &H800000 Else CRC32 = CRC32 \ &H100&
            CRC32 = CRC32 Xor CRC32Table(J)
        Next
        
        CRC32 = Not CRC32
    End Function
    
    Public Function BNLSChecksum(ByVal Password As String, ByVal ServerCode As Long) As Long
        BNLSChecksum = CRC32(Password & Right("0000000" & Hex(ServerCode), 8))
    End Function
    I got as far as,

    Code:
        def InitCRC32():
            if CRC32Initialized:
                pass
            CRC32Initialized = true
            while i == 0:255:
                K = i
                while J == 1:8:
                    if K and 1:
                        XorVal = CRC32_POLYNOMIAL
                    else:
                        XorVal = 0
                    if k < 0:
                        K = ((K and 0x7FFFFFFF)\2) or 0x40000000:
                    else:
                        K = K\2
                    K = K Xor XorVal
    I know this code won't work. If anyone can, please help me, this is getting very annoying.
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    What you are getting hung up on is that VB overloads the keywords AND and OR to perform both boolean and bitwise logic. When applied to an integer value, AND performs a bitwise comparison rather than a logical one; so if you do
    Code:
    a = 6 AND 3
    then the value of a will be 2; in detail, what it is doing is
    Code:
        0110
    AND 0011
        ----
        0010
    (This is somewhat simplified, as VB 6 Longs are 32 bits wide).

    Thus, in the line
    Code:
    If K And 1 Then
    what it is doing it determining if the first bit in K is set. Similarly with the references to OR and XOR when used with Integers. See this tutorial for more on this topic.

    According to the Python Reference Manual, the operators you need are '&' for AND, '|' for OR, and '^' for XOR. AFAIK, there is no built-in bitwise NOT; this function should have the correct result, however:
    Code:
    def bitNot(x):
    	return x ^ 0xFFFFFFFF;   # all bits set in a 32-bit integer
    Mind you, a direct translation of the VB code still may not work for other reasons, but this should at least put in on the right track. HTH.
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ok, thanks for the help so far, I'll see what I can do. I have a few more small questions though. How would i do something like Mid$(Data, 4)) in Python, would data[0:4] be good? And the last two question are; What does Python have that works like Visual Basic's function, Val(). And how do i reverse a string? I thought I remember seeing just stringname.reverse(). But that didn't work like I'd hoped.

IMN logo majestic logo threadwatch logo seochat tools logo