2016-12-17から1日間の記事一覧

Python | python コードのテスト

unittest を使う # utils.py def to_hex(integer, length, byteorder, *, signed=False): b = integer.to_bytes(length, byteorder, signed=signed) lst = ['{:02X}'.format(x) for x in b] return ''.join(lst) # test_to_hex.py from unittest import Test…

Python | 2の補数 (signed) int ⇔ bytes ⇔ str (16進文字列)

(signed) int → bytes int.to_bytes を使う >>> (-100).to_bytes(1, byteorder='little', signed=True) b'\x9c' >>> (-1000).to_bytes(2, 'little', signed=True) b'\x18\xfc' >>> (-1000).to_bytes(2, 'big', signed=True) b'\xfc\x18' struct.pack を使う …