開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Programming Bitcoin: Learn How to Program Bitcoin from Scratch (Jimmy Song(著)、O'Reilly Media)のChapter 4(Serialization)、Big- and Little-Endian Redux、Exercises 7(86)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3 from unittest import TestCase, main def little_endian_to_int(b: bytes) -> int: return int.from_bytes(b, 'little') class MyTestCase(TestCase): def setUp(self): pass def tearDown(self): pass def test_little_endian_to_int_1(self): self.assertEqual(little_endian_to_int(b'\x01\x00'), 16 ** 0 * 1) def test_little_endian_to_int_2(self): self.assertEqual(little_endian_to_int(b'\x00\x01'), 16 ** 2 * 1) if __name__ == '__main__': for b in [b'\x10\x00', b'\x00\x10']: for o in [b, little_endian_to_int(b)]: print(o) print() main()
入出力結果(cmd(コマンドプロンプト)、Terminal、Jupyter(IPython))
C:\Users\...>py sample7.py -v b'\x10\x00' 16 b'\x00\x10' 4096 test_little_endian_to_int_1 (__main__.MyTestCase) ... ok test_little_endian_to_int_2 (__main__.MyTestCase) ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK C:\Users\...>
0 コメント:
コメントを投稿