Difficulty using struct.pack format that works for both x86 and armv7

Viewed 62

I'm not sure if this is a library bug or if I'm missing something but the following code doesn't work:

import struct
import socket
struct.pack("<I", socket.CAN_EFF_FLAG)
struct.pack("<i", socket.CAN_EFF_FLAG)

on x86, the former works, the latter gives:

struct.error: 'i' format requires -2147483648 <= number <= 2147483647

On armv7l, the latter works, but the former gives

struct.error: argument out of range

Furthermore, hex(socket.CAN_EFF_FLAG) gives:

'-0x80000000' # on armv7l

and

'0x80000000' # on x86

If it's a library bug, I'm not even sure which component is the source of the bug. Is it the value of CAN_EFF_FLAG that's wrong?

The more I dig this hole, the more I'm perplexed: on arm, the sign of the constant is negative. I'm unable to get to a bare metal representation to see the two's complement encoding. bin(socket.CAN_EFF_FLAG) simply gives me the 32 bit number with a minus sign pre-prended.

(Note: I am currently mitigating this by redefining CAN_EFF_FLAG = abs(socket.CAN_EFF_FLAG). It's an ugly hack but it works).

0 Answers
Related