The general feeling is that python itself does not support binary, but it provides a module to make up for it, which is the struct module.
Python does not have a binary type, but it can store binary type data, that is, use the string string type to store binary data. This does not matter, because string is based on 1 byte.
import struct
a=12.34
#Convert a into binary
bytes=struct.pack('i',a)
At this time, bytes is a string string. The string is the same as the binary storage content of a in bytes.
Reverse operation
Existing binary data bytes, (actually a string), convert it back into python data type:
a,=struct .unpack('i',bytes)
Note that unpack returns a tuple
So if there is only one variable:
bytes=struct.pack('i' ,a)
Then, you need to do this when decoding
a,=struct.unpack('i',bytes) or (a,)=struct.unpack('i',bytes )
If you use a=struct.unpack('i',bytes) directly, then a=(12.34,) is a tuple instead of the original floating point number.
If it is composed of multiple data, it can be like this:
a='hello' b='world!' c=2 d=45.123 bytes=struct.pack('5s6sif',a,b,c,d)
The bytes at this time are data in binary form, and you can directly Write a file such as binfile.write(bytes)
Then, when we need it, we can read it out, bytes=binfile.read()
and then decode it into python through struct.unpack() Variable
a,b,c,d=struct.unpack('5s6sif',bytes)
'5s6sif' is called fmt, which is a format string, consisting of numbers and characters. 5s represents a string of 5 characters, 2i represents 2 integers, etc. The following are the available characters and types. ctype represents a one-to-one correspondence with the types in python.
C Type | Python | Number of bytes | |
---|---|---|---|
pad byte | no value | 1 | |
char | string of length 1 | 1 | |
signed char | integer | 1 | |
unsigned char | integer | 1 | |
_Bool | bool | 1 | |
short | integer | 2 | |
unsigned short | integer | 2 | |
int | integer | 4 | |
unsigned int | integer or long | 4 | |
long | integer | 4 | ##L |
long | 4 | ##q | |
long | 8 | Q | |
long | 8 | f | |
float | 4 | d | |
float | 8 | s | |
string | 1 | p | |
string | 1 | P | |
long |
##Last one Can be used to represent pointer types, occupying 4 bytes |
##@ | native | |
---|---|---|
= | native | standard According to the original number of bytes |
< | little-endian | standard can be based on the original number of bytes |
> | big-endian | |
network (= big-endian) | standard Based on the original number of bytes | |