int.from_bytes() バイト列から整数への変換
最終更新:2026-09-22
int.from_bytes() バイト列から整数への変換
int.from_bytes(bytes, byteorder) はバイト列を指定したバイト順で整数に変換する。
| カテゴリ | 数値型メソッド |
|---|---|
| 種類 | 組み込み型メソッド |
| Python バージョン | all |
📝 構文
int.from_bytes()
⚙️ 引数
| bytes 必須 | The bytes or bytearray to convert. |
|---|---|
| byteorder 必須 | Byte order: 'big' for most-significant first, 'little' for least-significant first. |
| signed 任意 | Whether to interpret the result as signed two's complement.(デフォルト値:False) |
戻り値:int. The integer decoded from bytes with the given byte order.
元のオブジェクトを変更するか:いいえ(新しいオブジェクトを返す)
💥 例外
ValueError— Raised when byteorder is invalid.TypeError— Raised when bytes is not a bytes-like object.
▶ 例
下のコードを編集し「実行」をクリックすると、出力パネルに結果が表示されます:
実行結果:
255
255
💡 関連するケース
さらに多くのケースは近日公開予定です。
❓ よくある質問
Qfrom_bytes() はクラスメソッドですか?
Aはい。int.from_bytes(b, 'big') のように直接呼び、インスタンスは不要です。
Qパラメータは?
A必須の bytes(bytes または bytearray)と byteorder('big'/'little')です。signed=True にすると 2 の補数で負数にデコードします。
Qto_bytes() との関係は?
A逆の操作です:オーバーフローがなければ、int.from_bytes(x.to_bytes(2, 'big'), 'big') は x と等しくなります。
Qネットワークバイトオーダーのデータはどうパースしますか?
Aネットワークプロトコルはほとんどがビッグエンディアンです:int.from_bytes(data, 'big')。