문제

I'm attempting to implement MD5 (for curiosity's sake) in Python/Java, and am effectively translating the wikipedia MD5 page's pseudocode into either language. First, I used Java, only to encounter frustration with its negative/positive integer overflow (because unsigned ints aren't an option, for-all integer,-2147483648 <= integer <= 2147483647). I then employed Python, after deciding that it's better suited for heavy numerical computation, but realized that I wouldn't be able to overcome the unsigned 32-bit integer requirement, either (as Python immediately casts wrapped ints to longs).

Is there any way to hack around Java/Python's lack of unsigned 32-bit integers, which are required by the aforementioned MD5 pseudocode?

도움이 되었습니까?

해결책 2

As a note beforehand - I don't know if this is a good solution, but it appears to give the behaviour you want.

Using the ctypes module, you can access the underlying low-level data-type directly, and hence have an unsigned int in Python.

Specifically, ctypes.c_uint:

>>> i = ctypes.c_uint(0)
>>> i.value -= 1
>>> i                                                            
c_uint(4294967295)
>>> i.value += 1
>>> i
c_uint(0)

This is arguably abuse of the module - it's designed for using C code easily from within Python, but as I say, it appears to work. The only real downside I can think of is that I assume ctypes is CPython specific.

다른 팁

Since all the operations are bitwise operations, they wouldn't suffer from sign extension (which would cause you problems), except for right shift.

Java has a >>> operator for this purpose.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top