December 11th, 2004, 11:41 AM
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.
I got as far as,
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
CRC32Table(i) = K
Private Function CRC32(ByVal Data As String) As Long
Dim i As Long, J As Long
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)
CRC32 = Not CRC32
Public Function BNLSChecksum(ByVal Password As String, ByVal ServerCode As Long) As Long
BNLSChecksum = CRC32(Password & Right("0000000" & Hex(ServerCode), 8))
I know this code won't work. If anyone can, please help me, this is getting very annoying.
CRC32Initialized = true
while i == 0:255:
K = i
while J == 1:8:
if K and 1:
XorVal = CRC32_POLYNOMIAL
XorVal = 0
if k < 0:
K = ((K and 0x7FFFFFFF)\2) or 0x40000000:
K = K\2
K = K Xor XorVal
December 11th, 2004, 02:47 PM
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
then the value of a will be 2; in detail, what it is doing is
(This is somewhat simplified, as VB 6 Longs are 32 bits wide).
Thus, in the line
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:
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.
return x ^ 0xFFFFFFFF; # all bits set in a 32-bit integer
December 11th, 2004, 03:07 PM
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.