小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Python字節(jié)流打包拆包

 內德維德04 2016-09-24


Python提供了一個struct模塊用于打包拆包

---------------------------------------------------------------------------

該模塊的主要的方法:

  struct.pack(fmt,v1,v2,.....)

    將v1,v2等參數(shù)的值進行一層包裝,包裝的方法由fmt指定。被包裝的參數(shù)必須嚴格符合fmt。最后返回一個包裝后的字符串。

例如:

>>>import   struct
>>>a = 20
>>>b = 200
>>>buff = struct.pack('ii',a,b)   #轉換成字節(jié)流,雖然還是字符串,但是可以用于封包傳輸
>>>print  len(buff)
8                                                   #可以看到長度為8個字節(jié),正好是兩個int型數(shù)據(jù)的長度
>>>print buff

                                                    #二進制是亂碼
>>>print repr(buff)
'\x14\x00\x00\x00\xc8\x00\x00\x00'                #其中十六進制的 0x00000014,0x000000c8分別表示20和200
>>>



  struct.unpack(fmt,string)

    解包。用pack打包,然后就可以用unpack解包了。返回一個由解包數(shù)據(jù)(string)得到的一個元組(tuple),即使僅有一個數(shù)據(jù)也會被解包成        元組。其中l(wèi)en(string) 必須等于 calcsize(fmt)


例如:

>>>struct.unpack('ii',buff)#接上面的例子已有打包好的數(shù)據(jù)buff

(20,200)

>>>


  struct.calcsize(fmt)

    這個就是用來計算fmt格式所描述的結構的大小。

例如:

>>>struct.calcsize('ii')

8

>>>


  struct.unpack_from(fmt,string,offset)

    這個也是用來解包,與struct.unpack(fmt,string)類似,只是從參數(shù)string的偏移offset位置開始讀

  struct.pack_into(fmt,string,offset,v1,v2,.....)

    這個也是用來打包,與struct.pack(fmt,v1,v2,.....)類似,只是從參數(shù)string的偏移offset位置開始寫




---------------------------------------------------------------------------

格式字符串(format string)由一個或多個格式字符(format characters)組成,對于這些格式字符的描述參照Python manual

如下:

FormatC TypePythonNotes
xpad byteno value
ccharstring of length 1
bsignedcharinteger
Bunsignedcharinteger
_Boolbool(1)
hshortinteger
Hunsignedshortinteger
iintinteger
Iunsignedintinteger or long
llonginteger
Lunsignedlonglong
qlonglonglong(2)
Qunsignedlonglonglong(2)
ffloatfloat
ddoublefloat
schar[]string
pchar[]string
Pvoid*long

---------------------------------------------------------------------------

一個例子

  

import struct# native byteorder buffer = struct.pack('ihb', 1, 2, 3) print repr(buffer) print struct.unpack('ihb', buffer) # data from a sequence, network byteorder data = [1, 2, 3] buffer = struct.pack('!ihb', *data)print repr(buffer) print struct.unpack('!ihb', buffer)

 

Output:

'\x01\x00\x00\x00\x02\x00\x03'
(1, 2, 3)
'\x00\x00\x00\x01\x00\x02\x03'
(1, 2, 3)

首先將參數(shù)1,2,3打包,打包前1,2,3明顯屬于python數(shù)據(jù)類型中的integer,pack后就變成了C結構的二進制串,轉成python的string類型來顯示就是  '\x01\x00\x00\x00\x02\x00\x03'。由于本機是小端('little-endian',關于大端和小端的區(qū)別請參照Google),故而高位放在低地址段。i 代表C struct中的int類型,故而本機占4位,1則表示為01000000;h 代表C struct中的short類型,占2位,故表示為0200;同理b 代表C struct中的signed char類型,占1位,故而表示為03。


---------------------------------------------------------------------------

在Format string 的首位,有一個可選字符來決定大端和小端,列表如下:

CharacterByte orderSize and alignment
@nativenative
=nativestandard
<little-endianstandard
>big-endianstandard
!network (= big-endian)standard

如果沒有附加,默認為@,即使用本機的字符順序(大端or小端),對于C結構的大小和內存中的對齊方式也是與本機相一致的(native),比如有的機器integer為2位而有的機器則為四位;有的機器內存對其位四位對齊,有的則是n位對齊(n未知,我也不知道多少)。

還有一個標準的選項,被描述為:如果使用標準的,則任何類型都無內存對齊。

比如剛才的小程序的后半部分,使用的format string中首位為!,即為大端模式標準對齊方式,故而輸出的為'\x00\x00\x00\x01\x00\x02\x03',其中高位自己就被放在內存的高地址位了。




    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多