Skip to content Skip to sidebar Skip to footer

Syntax Invalid While Translating C Code To Python

I have C snippet(decompiled from IDA) to be translated to Python: # v29 = 0; # v30 = -1342924972; # while ( v29 < v62 ) // v62 is length of string to be decoded # { #

Solution 1:

As mentioned, hex returns a string, which obviously does not support bitwise operations like & with a numeric type:

>>> type(hex(3))
<class'str'>
>>> hex(3) & 0xf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'str'and'int'

The ord function already returns an int, so you can just remove the hex function altogether:

>>>ord('c') & 0xff
99

Solution 2:

ord is already an int, you don't need the hex, which returns a string.

And I may be wrong, but this line may give you problems

bstr[v29] = v33 & 0xff

You'd need to cast it to string again:

bstr[v29] = chr(v33 & 0xff)

Post a Comment for "Syntax Invalid While Translating C Code To Python"