I'm involved in a project where we're binding a C API into Jython (through Java). We've run into issues with unsigned values (since Java doesn't support them). We can use casting between Java and C, but moving from Jython to Java is a harder task.

I wrote some 'casting' functions in Python. They convert a bit pattern representing a signed or unsigned value into the SAME BIT PATTERN representing the opposite sign.

For example:

>>> u2s(0xFFFFFFFF) -1L >>> hex(s2u(-1)) '0xffffffffL'

Is there a more elegant way to handle these sorts of sign conversions between Jython and Java? Has any one tried to do this before?

Here's the entire module:

__all__ = ['u2s', 's2u'] def u2s(v,width=32): """ Convert a bit pattern representing an unsigned value to the SAME BIT PATTERN representing a signed value. >>> u2s(0xFFFFFFFF) -1L >>> u2s(0x7FFFFFFF) 2147483647L """ msb = int("1" + ((width - 1) * '0'), 2) msk = int("1" * width, 2) nv = v & msk if 0 < (msb & nv): return -1 * ((nv ^ msk) + 1) else: return nv def s2u(v,width=32): """ Convert a bit pattern representing a signed value to the SAME BIT PATTERN representing an unsinged value. >>> hex(s2u(-1)) '0xffffffffL' >>> hex(s2u(1)) '0x1L' """ msk = int("1" * width, 2) if 0 > v: return msk & (((-1 * v) ^ msk) + 1) else: return msk & v if __name__ == "__main__": import doctest doctest.testmod()

I went and benchmarked my code VS the accepted answer in Jython. The accepted answer performs about 1/3 better! I only tested the version with explicitly defined widths.

Edit my supplied code with the following to run the benchmark for yourself: