-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0066_binary_file.py
More file actions
33 lines (27 loc) · 940 Bytes
/
0066_binary_file.py
File metadata and controls
33 lines (27 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
with open('./data/binary', mode='bw') as bin_file:
for i in range(17):
bin_file.write(bytes([i]))
with open('./data/binary', mode='br') as bin_file:
for b in bin_file:
print(b)
a = 65534 # FF FE
b = 65535 # FF FF
c = 65536 # 00 01 00 00
d = 2998302 # 00 2D C0 1E
with open('./data/binary2', mode='bw') as bin_file:
bin_file.write(a.to_bytes(2, 'big'))
bin_file.write(b.to_bytes(2, 'big'))
bin_file.write(c.to_bytes(4, 'big'))
bin_file.write(d.to_bytes(4, 'big'))
bin_file.write(d.to_bytes(4, 'little')) # big endian or little endian
with open('./data/binary2', mode='br') as bin_file:
e = int.from_bytes(bin_file.read(2), 'big')
print(e)
f = int.from_bytes(bin_file.read(2), 'big')
print(f)
g = int.from_bytes(bin_file.read(4), 'big')
print(g)
h = int.from_bytes(bin_file.read(4), 'big')
print(h)
i = int.from_bytes(bin_file.read(4), 'big')
print(i)