"Jack" <[EMAIL PROTECTED]> wrote: > This is a naive question: > > "%u" % -3 > > I expect it to print 3. But it still print -3.
Internally it uses the C runtime to format the number, but if the number you ask it to print unsigned is negative it uses %d instead of %u. I have no idea if it is actually possibly to get a different output for %d versus %u. > > Also, if I have an int, I can convert it to unsigned int in C: > int i = -3; > int ui = (unsigned int)i; > > Is there a way to do this in Python? > Depeneding on how exactly you want it converted: i = -3 ui = abs(i) print ui ui = (i & 0xffff) # for 16 bit integers print ui ui = (i & 0xffffffff) # for 32 bit integers print ui ui = (i & 0xffffffffffffffff) # for 64 bit integers print ui ui = (i & 0xffffffffffffffffffffffffffffffff) # for 128 bit integers print ui which gives the following output: 3 65533 4294967293 18446744073709551613 340282366920938463463374607431768211453 There isn't a unique way to convert a Python integer to an unsigned value which is why the %u format string cannot do anything other than print the value. Personally I'd have expected the Python one to either print the absolute value or throw an exception, but I guess making it an alias for %d kind of makes sense as well. -- http://mail.python.org/mailman/listinfo/python-list