From | To | Expression |
---|---|---|
45 | "45" | str(data) |
45 | "101101" | bin(data) |
45 | "2D" | hex(data) |
45 | "\x00\x00\x00\x2d" | struct.pack('!i', data) |
"45" | 45 | int(data) |
"45" | "3435" | data.encode('hex') |
"101101" | 45 | int(data, 2) |
"2D" | 45 | int(data, 16) |
"2D" | "\x2d" | binascii.unhexlify(data) or data.decode('hex') |
"\x00\x00\x00\x2d" | 45 | struct.unpack('!i', data)[0] |
"\x2d" | "2D" | binascii.hexlify(data) |
"3435" | "45" | data.decode('hex') |
Comments are welcome here or in my original blog post regarding this table.
@Caustic What I'm missing is the precisely what Python's
str
type really means. Do Python strings actually correspond directly to a specific representation as a bit array (rather than the more abstract notion of a unicode string that I'm used to on the JVM) - so "\x00\x00\x00\2d" doesn't strictly denote a 4-character string like I thought, but rather some 32 bits with no particular semantics attached?