QUERY · ISSUE
int.to_bytes and int.from_bytes: signed parameter not implemented
bugpy-core
Port, board and/or hardware
esp32-WROOM
MicroPython version
MicroPython v1.23.0 on 2024-06-02; Generic ESP32 module with ESP32
Reproduction
in micropython
int.from_bytes(b'\xFF\xFF' , 'big' , True) ---> 65535
in Python 3.11.4
int.from_bytes(b'\xFF\xFF' , byteorder='big' , signed=True) ---> -1
Expected behaviour
Expected to return -1
according to https://docs.python.org/3/library/stdtypes.html#int.from_bytes
Observed behaviour
returns 65535 which is equivalent to signed=False
Additional Information
No, I've provided everything above.
Code of Conduct
Yes, I agree
CANDIDATE · ISSUE
implement bit_length() for mp_type_int
py-core
int.to_bytes(size, byteorder) requires one to know how many bytes - size - the output is but int.bit_length() is not implemented.
>>> (1024).to_bytes(2, byteorder='big')
b'\x04\x00'
>>> (1024).to_bytes(10, byteorder='big')
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> (-1024).to_bytes(10, byteorder='big', signed=True)
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes((x.bit_length() + 7) // 8, byteorder='little')
b'\xe8\x03'