[Tutor] Problems with unsigned integers

Kent Johnson kent37 at tds.net
Mon Dec 13 19:27:25 CET 2004


It seems that ntohl doesn't understand about unsigned values, at least on Win32:

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> from struct import pack, unpack
 >>> pack('L', -1)
'\xff\xff\xff\xff'
 >>> unpack('L', '\xff\xff\xff\xff')
(4294967295L,)
 >>> from socket import ntohl
 >>> ntohl(4294967295L)
-1

Kent


Loptr Chaote wrote:
> Hello everyone! 
> 
> I'm having problems with signed/unsigned (32bit) integers in python.
> Example code:
> 
>   seq = 0L
>   seq = socket.ntohl(struct.unpack("L", data[38:42])[0])
>   print seq
> 
> This sometimes produces a negative output, how is that possible since
> I booth initialized seq with "0L" and also specified "L" in unpack()?
> 
> -L.C
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list