Dav*_*ave 0 c python sockets htonl
我试图模仿Python中C程序某些部分的行为.他们都在读取二进制文件,其中每个记录都是length-> data.C程序将从文件中提取长度字节,无论是正还是负,ntohl()都将返回正确的长度.但是,在Python中,即使长度记录是相同的值,当它为负时我得到一个OverflowError:
长度:419495936
ntohl:281
长度:-2147418112
OverflowError:无法将负数转换为无符号长整数
与C相比:
长度:419495936
ntohl:281
长度:-2147418112
ntohl:384
我在这做错了什么?如何让Python产生与C相同的结果?
用代码编辑
这是C代码:
while (fread(&rec_len, sizeof(unsigned int), 1, fp1) != 0) {
printf("length: %d\n", rec_len);
rec_len = ntohl(rec_len);
printf("ntohl: %d\n", rec_len);
Run Code Online (Sandbox Code Playgroud)
这是Python:
with open(file, "rb") as f:
pSize = struct.unpack('<i', f.read(4))[0]
print "length: " + str(pSize)
packetSize = socket.ntohl(pSize)
print "ntohl: " + str(packetSize)
while packetSize > 0:
packet = f.read(packetSize)
pSize = struct.unpack('<i', f.read(4))[0]
print "length: " + str(pSize)
packetSize = socket.ntohl(pSize)
print "ntohl: " + str(packetSize)
Run Code Online (Sandbox Code Playgroud)
您需要将正数传递给htonl().如果您无法更改读取值的方式,则可以使用以下代码:
>>> socket.htonl((-2147418112) & 0xffffffff)
384L
Run Code Online (Sandbox Code Playgroud)
编辑:看了你的代码,你可以改变struct.unpack使用'<I'它给你一个无符号数,甚至使用'>I'或'!I'否定后来需要ntohl().