Day 70 TCP stick package python learning solutions struct module

Next, we first introduce the use of lower module can solve the sticky package:

struct

The python struct module provides such a mechanism, the main role is to block the conversion python primitive values ​​between C struct type represented by the python string format (This module performs conversions between Python values ​​and C structs represented as Python strings.). stuct module provides several functions very simple, write the following examples.

Two functions: pack (), unpack ()

struct module two most important functions is the Pack () , unpack () method

Packaging function: Pack (fmt, v1, v2, v3, ...)

Unpack function: the unpack (FMT, Buffer)

 

struct Import 
# Pack digital conversion of arbitrary length to a fixed length of four bytes of the byte stream
# unpack values are restored into the original 4-byte data, and returns a tuple

res = struct.pack ( "i" , 2372722) # int can not exceed the range
Print (RES)
Print (len (RES))

RES = struct.unpack ( "I", RES)
Print (RES)
Print (RES [0], type (RES [0]))

Guess you like

Origin www.cnblogs.com/jianchixuexu/p/11892650.html
Recommended