Python struct module

Source: Internet
Author: User
Tags unpack

1 Importstruct#a module2 3 4 #Functions to convert between Python values and C structs.5 #Python bytes Objects is used to hold the data representing the C struct6 #and also as format strings (explained below) to describe the layout of the data in the C struct.7 #@: Native order, Size & Alignment (default)8 #=: Native order, std. Size & Alignment9 #<: Little-endian, std. Size & AlignmentTen #: Big-endian, std. Size & Alignment One #!: Same as > A  - #X:pad byte (no data); c:char; b:signed byte; b:unsigned byte; - #?: _bool (requires C99; if not available, char is used instead) the #H:short; h:unsigned Short; I:int; i:unsigned int; - #L:long; L:unsigned long; F:float; d:double; E:half-float. Special cases (preceding decimal count indicates length): - #s:string (array of char); P:pascal string (with Count byte). Special cases (only available in native format): - #n:ssize_t; n:size_t; + #P:an integer type is wide enough to hold a pointer. Special case (not in native mode unless ' long long ' in platform C): - #Q:long Long; q:unsigned Long Long + #class struct (object) struct (FMT)---Compiled struct object A #Return A new Struct object which writes and reads binary data according to the format string FMT at  - #iter_unpack (...) S.iter_unpack (buffer), iterator (v1, v2, ...) - # - #Pack_into (...) S.pack_into (buffer, offset, v1, v2, ...) - #Unpack_from (...) S.unpack_from (buffer, offset=0) (v1, v2, ...) - # in #Pack (...) S.pack (v1, v2, ...) bytes - #Unpack (...) S.unpack (buffer) (v1, v2, ...) to  +  - Importstruct the  *  $ #1, Struct.packPanax NotoginsengA = 20 -B = 400 the  +Str_ = Struct.pack (">ii", A, B)#the converted str, although a string type, is equivalent to a stream of bytes in other languages (a byte array) that can be transmitted over the network A Print('Length:', Len (str_)) the Print('Type:', type (STR_)) + Print(STR_) - Print(repr (str_)) $  $ #Length:8 - #type: <class ' bytes ' > - #b ' \x00\x00\x00\x14\x00\x00\x01\x90 ' the #b ' \x00\x00\x00\x14\x00\x00\x01\x90 ' - Wuyi  theStr_ = Struct.pack ("<ii", A, B)#the converted str, although a string type, is equivalent to a stream of bytes in other languages (a byte array) that can be transmitted over the network - Print('Length:', Len (str_)) Wu Print('Type:', type (STR_)) - Print(STR_) About Print(repr (str_)) $  - #Length:8 - #type: <class ' bytes ' > - #b ' \x14\x00\x00\x00\x90\x01\x00\x00 ' A #b ' \x14\x00\x00\x00\x90\x01\x00\x00 ' +  the  - #2, Struct.unpack $A1, a2 = Struct.unpack ("<ii", STR_)#return to progenitor the Print(A1, A2)#20,400 the  the  the #3. Struct.calcsize used to calculate the length of the result of the format string - Print(Struct.calcsize ('>ii'))#8 in  the  the #4, Struct.pack_into, Struct.unpack_from About  fromcTYPESImportCreate_string_buffer the  theBUF = Create_string_buffer (12) the Print(Repr (Buf.raw))#b ' \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 ' +  -Struct.pack_into (">III", buf, 0, 1, 2,-1)#0 is offset the Print(Repr (Buf.raw))#b ' \x01\x00\x00\x00\x02\x00\x00\x00\xff\xff\xff\xff 'Bayi  the Print(Struct.unpack_from ('>III', buf, 0))#(1, 2,-1) the  - #5, Struct.iter_unpack -ret = Struct.iter_unpack ('>III', BUF) the Print(ret)#<unpack_iterator object at 0x0000000001d02e18> the  forRinchret: the     Print(r)#(1, 2,-1) the  -  the #6. Practice the Print(Struct.pack ('ci'B'*', 0x12131415))#b ' *\x00\x00\x00\x15\x14\x13\x12 ' the Print(Struct.pack ('IC', 0x12131415, b'*'))#b ' \x15\x14\x13\x12* '94  theRecord = b'Raymond \x32\x12\x08\x01\x08' the Print(Struct.unpack ('<10SHHB', record))#(b ' Raymond ', 4658, 8)
character byte order size Snap To
@ Natively Natively Natively
= Natively Standard No
< Small end Standard No
> Big End Standard No
! Network (equals big-endian) Standard No
format Type C python type Standard size Tips
X Padding bytes No value
C Char 1 bytes in length 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 Integet 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

Python struct module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.