本文研究的主要是Python內(nèi)建模塊struct的相關(guān)內(nèi)容,具體如下。
Python中變量的類(lèi)型只有列表、元祖、字典、集合等高級(jí)抽象類(lèi)型,并沒(méi)有像c中定義了位、字節(jié)、整型等底層初級(jí)類(lèi)型。因?yàn)镻ython本來(lái)就是高級(jí)解釋性語(yǔ)言,運(yùn)行的時(shí)候都是經(jīng)過(guò)翻譯后再在底層運(yùn)行。如何打通Python和其他語(yǔ)言之間的類(lèi)型定義障礙,Python的內(nèi)建模塊struct完全解決了所有問(wèn)題。
在struct模塊中最最常用的三個(gè): 
(1)struct.pack:用于將Python的值根據(jù)格式符,轉(zhuǎn)換為字符串(因?yàn)镻ython中沒(méi)有字節(jié)(Byte)類(lèi)型,可以把這里的字符串理解為字節(jié)流,或字節(jié)數(shù)組)。 
(2)struct.unpack: 剛好與struct.pack相反,用于將字節(jié)流轉(zhuǎn)換成python數(shù)據(jù)類(lèi)型,該函數(shù)返回一個(gè)元組。 
(3)struct.calcsize: 計(jì)算格式字符串所對(duì)應(yīng)的結(jié)果的長(zhǎng)度。
轉(zhuǎn)換過(guò)程中遇到的格式操作:
| 格式符 | C語(yǔ)言類(lèi)型 | Python類(lèi)型 | 
|---|---|---|
| x | pad byte | no value | 
| c | char | string of length 1 | 
| b | signed char | integer | 
| B | unsigned char | integer | 
| ? | _Bool | bool | 
| h | short | integer | 
| H | unsigned short | integer | 
| i | int | integer | 
| I | unsigned int | integer or long | 
| l | long | integer | 
| L | unsigned long | long | 
| q | long long | long | 
| Q | unsigned long long | long | 
| f | float | float | 
| d | double | float | 
| s | char[] | string | 
| p | char[] | string | 
| P | void * | long | 
#!/usr/bin/python# -*- coding:utf-8 -*-'''測(cè)試struct模塊'''from struct import *import arraydef fun_calcsize():  print 'ci:',calcsize('ci')#計(jì)算格式占內(nèi)存大小  print '@ci:',calcsize('@ci')  print '=ci:',calcsize('=ci')  print '>ci:',calcsize('>ci')  print '<ci:',calcsize('<ci')  print 'ic:',calcsize('ic')#計(jì)算格式占內(nèi)存大小  print '@ic:',calcsize('@ic')  print '=ic:',calcsize('=ic')  print '>ic:',calcsize('>ic')  print '<ic:',calcsize('<ic')def fun_pack(Format,msg = [0x11223344,0x55667788]):  result = pack(Format,*msg)  print 'pack'.ljust(10),str(type(result)).ljust(20),  for i in result:    print hex(ord(i)), # ord把ASCII碼表中的字符轉(zhuǎn)換成對(duì)應(yīng)的整形,hex將數(shù)值轉(zhuǎn)化為十六進(jìn)制  print  result = unpack(Format,result)  print 'unpack'.ljust(10),str(type(result)).ljust(20),  for i in result:    print hex(i),  print def fun_pack_into(Format,msg = [0x11223344,0x55667788]):  r = array.array('c',' '*8)#大小為8的可變緩沖區(qū),writable buffer  result = pack_into(Format,r,0,*msg)  print 'pack_into'.ljust(10),str(type(result)).ljust(20),  for i in r.tostring():    print hex(ord(i)),  print  result = unpack_from(Format,r,0)  print 'pack_from'.ljust(10),str(type(result)).ljust(20),  for i in result:    print hex(i),  printdef IsBig_Endian():  '''判斷本機(jī)為大/小端'''  a = 0x12345678  result = pack('i',a)#此時(shí)result就是一個(gè)string字符串,字符串按字節(jié)同a的二進(jìn)制存儲(chǔ)內(nèi)容相同。  if hex(ord(result[0])) == '0x78':    print '本機(jī)為小端'  else:    print '本機(jī)為大端'def test():  a = '1234'  for i in a:    print '字符%s的二進(jìn)制:'%i,hex(ord(i))#字符對(duì)應(yīng)ascii碼表中對(duì)應(yīng)整數(shù)的十六進(jìn)制  '''  不用unpack()返回的數(shù)據(jù)也是可以使用pack()函數(shù)的,只要解包的字符串符合解包格式即可,  pack()會(huì)按照解包格式將字符串在內(nèi)存中的二進(jìn)制重新解釋(說(shuō)的感覺(jué)不太好...,見(jiàn)下例)  '''  print '大端:',hex(unpack('>i',a)[0])#因?yàn)閜ack返回的是元組,即使只有一個(gè)元素也是元組的形式  print '小端:',hex(unpack('<i',a)[0])if __name__ == "__main__":  print '判斷本機(jī)是否為大小端?',  IsBig_Endian()  fun_calcsize()  print '大端:'  Format = ">ii"  fun_pack(Format)  fun_pack_into(Format)  print '小端:'  Format = "<ii"  fun_pack(Format)  fun_pack_into(Format)  print 'test'  test()  '''  result:  判斷本機(jī)是否為大小端? 本機(jī)為小端  ci: 8  @ci: 8  =ci: 5  >ci: 5  <ci: 5  ic: 5  @ic: 5  =ic: 5  >ic: 5  <ic: 5  大端:  pack    <type 'str'>     0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88  unpack   <type 'tuple'>    0x11223344 0x55667788  pack_into <type 'NoneType'>  0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88  pack_from <type 'tuple'>    0x11223344 0x55667788  小端:  pack    <type 'str'>     0x44 0x33 0x22 0x11 0x88 0x77 0x66 0x55  unpack   <type 'tuple'>    0x11223344 0x55667788  pack_into <type 'NoneType'>  0x44 0x33 0x22 0x11 0x88 0x77 0x66 0x55  pack_from <type 'tuple'>    0x11223344 0x55667788  test  字符1的二進(jìn)制: 0x31  字符2的二進(jìn)制: 0x32  字符3的二進(jìn)制: 0x33  字符4的二進(jìn)制: 0x34  大端:0x31323334  小端:0x34333231  '''            
新聞熱點(diǎn)
疑難解答
圖片精選