pythonバイト順

3411 ワード

チェン1ピーク-バイトシーケンスの理解
バイト順の取得
import sys

endianness = sys.byteorder
print("system Endianness is "+ endianness)

サイズ端変換

"6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000".decode('hex')[::-1].encode('hex_codec') 
#=> 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
import struct

ver = 1
prev_block = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
mrkl_root = "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
time = 1231469665
bits = 486604799
nonce = 2573394689

struct.pack("

struct
structには以下のバイト順があります
Character
Byte order
Size
Alignment
@
native
native
native
=
native
standard
none
<
little-endian
standard
none
>
big-endian
standard
none
!
network (= big-endian)
standard
none
文字
バイト順
サイズ
配置
@
ネイティブ
ネイティブ
ネイティブ
=
ネイティブ
標準
なし
<
小端
標準
なし
>
大端
標準
なし
!
ネットワーク・エンド
標準
なし
データフォーマット
Format
C Type
Python type
Standard size
Notes
x
pad byte
no value
c
char
bytes of length 1
1
b
signed char
integer
1
(1),(3)
B
unsigned char
integer
1
(3)
?
_Bool
bool
1
(1)
h
short
integer
2
(3)
H
unsigned short
integer
2
(3)
i
int
integer
4
(3)
I
unsigned int
integer
4
(3)
l
long
integer
4
(3)
L
unsigned long
integer
4
(3)
q
long long
integer
8
(2), (3)
Q
unsigned long
long
integer
8
(2), (3)
n
ssize_t
integer
(4)
N
size_t
integer
(4)
e
(7)
float
2
(5)
f
float
float
4
(5)
d
double
float
8
(5)
s
char[]
bytes
p
char[]
bytes
P
void *
integer
(6)
文字
Cタイプ
pythonタイプ
ひょうじゅんすんぽう
x
パディングバイト
意味のない値
c
char
長さ1バイト
1
b
signed char
せいけい
1
B
unsigned char
せいけい
1
?
_Bool
ブール
1
h
short
せいけい
2
H
unsigned short
せいけい
2
i
int
せいけい
4
I
unsigned int
せいけい
4
l
long
せいけい
4
L
unsigned long
せいけい
4
q
long long
せいけい
8
Q
unsigned long long
せいけい
8
n
ssize_t
せいけい
N
size_t
せいけい
e
フローティング
2
f
float
フローティング
4
d
double
フローティング
8
s
char[]
バイト
p
char[]
バイト
P
void *
せいけい
参照先:
https://en.wikipedia.org/wiki/Endianness https://docs.python.org/3/library/struct.html https://blog.csdn.net/youand_me/article/details/78890316