2017-07-03 18:05:08 -04:00
|
|
|
import skip_if
|
|
|
|
skip_if.no_bigint()
|
|
|
|
|
2017-03-03 16:13:27 -05:00
|
|
|
print((2**64).to_bytes(9, "little"))
|
2020-02-14 15:12:20 -05:00
|
|
|
print((-2**64).to_bytes(9, "little", signed=True))
|
2017-06-14 23:56:21 -04:00
|
|
|
print((2**64).to_bytes(9, "big"))
|
2020-02-14 15:12:20 -05:00
|
|
|
print((-2**64).to_bytes(9, "big", signed=True))
|
2017-03-03 16:13:27 -05:00
|
|
|
|
2017-01-21 12:15:31 -05:00
|
|
|
b = bytes(range(20))
|
|
|
|
|
|
|
|
il = int.from_bytes(b, "little")
|
|
|
|
ib = int.from_bytes(b, "big")
|
|
|
|
print(il)
|
|
|
|
print(ib)
|
|
|
|
print(il.to_bytes(20, "little"))
|
2017-06-14 23:56:21 -04:00
|
|
|
print(ib.to_bytes(20, "big"))
|
2017-04-24 22:07:02 -04:00
|
|
|
|
|
|
|
# check that extra zero bytes don't change the internal int value
|
|
|
|
print(int.from_bytes(b + bytes(10), "little") == int.from_bytes(b, "little"))
|
2019-05-09 03:19:29 -04:00
|
|
|
|
|
|
|
# too small buffer should raise an error
|
|
|
|
try:
|
|
|
|
(2**64).to_bytes(8, "little")
|
|
|
|
except OverflowError:
|
|
|
|
print("OverflowError")
|
|
|
|
|
2020-02-14 15:12:20 -05:00
|
|
|
# negative numbers should raise an error if signed=False
|
2019-05-09 03:19:29 -04:00
|
|
|
try:
|
|
|
|
(-2**64).to_bytes(9, "little")
|
|
|
|
except OverflowError:
|
|
|
|
print("OverflowError")
|
2020-02-14 15:12:20 -05:00
|
|
|
try:
|
|
|
|
(-2**64).to_bytes(9, "little", signed=False)
|
|
|
|
except OverflowError:
|
|
|
|
print("OverflowError")
|