Replies: 1 comment
16 進数を扱う Python APIgraph TD
%% ノードの定義
STR["文字列<br/>'Hello world!'"]
BYTES_OBJ["バイト列<br/>b'Hello world!'"]
HEX_STR_NO_PREFIX["16進数文字列<br/>(プレフィックスなし)<br/>'48656c6c6f20776f726c6421'"]
HEX_STR_PREFIX["16進数文字列<br/>(プレフィックス付き)<br/>'0x48656c6c6f20776f726c6421'"]
HEX_BYTES["16進数バイト列<br/>b'48656c6c6f20776f726c6421'"]
INT["整数<br/>87521618088882533792115812"]
%% 文字列からバイト列への変換
STR -->|"s.encode('utf-8')<br/>s.encode('ascii')"| BYTES_OBJ
%% バイト列から文字列への逆変換
BYTES_OBJ -->|"b.decode('utf-8')<br/>b.decode('ascii')"| STR
%% バイト列から16進数文字列への変換
BYTES_OBJ -->|"bytes.hex()<br/>binascii.hexlify().decode()"| HEX_STR_NO_PREFIX
%% 16進数文字列からバイト列への逆変換
HEX_STR_NO_PREFIX -->|"bytes.fromhex()<br/>binascii.unhexlify()"| BYTES_OBJ
%% バイト列と16進数バイト列の変換
BYTES_OBJ -->|"binascii.hexlify()"| HEX_BYTES
HEX_BYTES -->|"binascii.unhexlify()"| BYTES_OBJ
%% 16進数バイト列から文字列への変換
HEX_BYTES -->|"decode()"| HEX_STR_NO_PREFIX
HEX_STR_NO_PREFIX -->|"encode()"| HEX_BYTES
%% バイト列から整数への変換
BYTES_OBJ -->|"int.from_bytes(bytes, 'big')<br/>int.from_bytes(bytes, 'little')"| INT
%% 整数からバイト列への変換
INT -->|"x.to_bytes(length, 'big')<br/>x.to_bytes(length, 'little')"| BYTES_OBJ
%% 整数から16進数文字列への変換
INT -->|"hex()"| HEX_STR_PREFIX
INT -->|"format(x, 'x')<br/>f#quot;{x:x}#quot;"| HEX_STR_NO_PREFIX
%% 16進数文字列から整数への変換
HEX_STR_PREFIX -->|"int(s, 16)"| INT
HEX_STR_NO_PREFIX -->|"int(s, 16)"| INT
%% スタイリング
classDef strNode fill:#fff2cc,stroke:#d6b656,stroke-width:2px,color:#000000
classDef bytesNode fill:#e8f5e8,stroke:#1b5e20,stroke-width:2px,color:#000000
classDef hexStrNode fill:#f3e5f5,stroke:#4a148c,stroke-width:2px,color:#000000
classDef intNode fill:#e1f5fe,stroke:#01579b,stroke-width:2px,color:#000000
class STR strNode
class BYTES_OBJ bytesNode
class HEX_STR_NO_PREFIX,HEX_STR_PREFIX,HEX_BYTES hexStrNode
class INT intNode
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Today I Learned ...
All reactions