python中struct.pack中的fmt理解(筆記)


我們知道python只定義了6種數據類型,字符串,整數,浮點數,列表,元組,字典。但是C語言中有些字節型的變量,在python中該如何實現呢?這點頗為重要,特別是要在網絡上進行數據傳輸的話。

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

Return a string containing the values v1, v2, … packed according to the given format. The arguments must match the values required by the format exactly.

野生翻譯:返回一個包含v1,v2的,根據所給fmt打包的字符串,其中的參數必須和fmt要求的值匹配

重點來理解一下fmt:

一、下面是講fmt的第一個字符:
By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler).
野生翻譯:默認情況下,C類型在機器的本機格式和字節順序中表示,如果需要的話,可以通過跳過pad字節來正確地對齊(根據C編譯器使用的規則)。

Alternatively, the first character of the format string can be used to indicate the byte order, size and alignment of the packed data, according to the following table:
野生翻譯:可以使用格式字符串的第一個字符來指示填充數據的字節順序、大小和對齊方式(這是可選的),如下表所示

這里寫圖片描述
If the first character is not one of these, ‘@’ is assumed.
野生翻譯:如果第一個字符不是上面表里面中的一個,@是默認的值

Native byte order is big-endian or little-endian, depending on the host system. For example, Intel x86 and AMD64 (x86-64) are little-endian; Motorola 68000 and PowerPC G5 are big-endian; ARM and Intel Itanium feature switchable endianness (bi-endian). Use sys.byteorder to check the endianness of your system.
野生翻譯:原生字節順序是big-endian還是little-endian,取決於主機系統。例如,Intel x86和AMD64(x86-64)是little-endian ;摩托羅拉68000和PowerPC G5是big-endian; ARM和英特爾的Itanium特性是可切換的(雙endian)。可以使用sys.byteorder指令來檢查你系統是按照什么排序的

Native size and alignment are determined using the C compiler’s sizeof expression. This is always combined with native byte order.

Standard size depends only on the format character; see the table in the Format Characters section.

Note the difference between ‘@’ and ‘=’: both use native byte order, but the size and alignment of the latter is standardized.

The form ‘!’ is available for those poor souls who claim they can’t remember whether network byte order is big-endian or little-endian.

There is no way to indicate non-native byte order (force byte-swapping); use the appropriate choice of ‘<’ or ‘>’.

野生翻譯:本機大小和對齊是使用C編譯器的sizeof表達式來確定的。這總是與本機字節順序相結合。
標准大小只取決於格式字符;請參見格式字符部分中的表格。
注意“@”和“=”之間的區別:兩者都使用本機字節順序,但是后者的大小和對齊是標准化的。
表單”!“對於那些不記得網絡字節順序是big-endian或little-endian的字節順序的人來說是可行的。”
沒有辦法指出非本地字節順序(強制字節交換);使用“<”或“>”的適當選擇。

Notes:

  1. Padding is only automatically added between successive structure members. No padding is added at the beginning or the end of the encoded struct.
  2. No padding is added when using non-native size and alignment, e.g. with ‘<’, ‘>’, ‘=’, and ‘!’.
  3. To align the end of a structure to the alignment requirement of a particular type, end the format with the code for that type with a repeat count of zero. See Examples.

野生翻譯:
注意:

  1. 填充只在連續的結構成員之間自動添加。在編碼的結構體的開始或結束時沒有添加填充物。
  2. 在使用非本地大小和對齊方式時,不添加任何填充,例如“<”、“”、“=”和“!”。
  3. 為了使結構的結束與特定類型的對齊要求保持一致,以重復計數為零的方式結束該類型的代碼。詳見examples

二、 下面是講fmt后面的內容
Format characters have the following meaning; the conversion between C and Python values should be obvious given their types. The ‘Standard size’ column refers to the size of the packed value in bytes when using standard size; that is, when the format string starts with one of ‘<’, ‘>’, ‘!’ or ‘=’. When using native size, the size of the packed value is platform-dependent.

野生翻譯:fmt字符有以下含義;C和Python值之間的轉換應該是明顯的,因為它們提供的類型。“標准大小”列指的是在使用標准尺寸時以字節為單位的包裝值的大小;也就是說,當fmt以“<”、“”、“!”開頭的時候。”或“=”。當使用本機大小時,包裝值的大小與平台相關。

這里寫圖片描述
Notes:

  1. The ‘?’ conversion code corresponds to the _Bool type defined by C99. If this type is not available, it is simulated using a char. In standard mode, it is always represented by one byte.

New in version 2.6.

  1. The ‘q’ and ‘Q’ conversion codes are available in native mode only if the platform C compiler supports C long long, or, on Windows, __int64. They are always available in standard modes.

New in version 2.2.

  1. When attempting to pack a non-integer using any of the integer conversion codes, if the non-integer has a index() method then that method is called to convert the argument to an integer before packing. If no index() method exists, or the call to index() raises TypeError, then the int() method is tried. However, the use of int() is deprecated, and will raise DeprecationWarning.

Changed in version 2.7: Use of the index() method for non-integers is new in 2.7.

Changed in version 2.7: Prior to version 2.7, not all integer conversion codes would use the int() method to convert, and DeprecationWarning was raised only for float arguments.

  1. For the ‘f’ and ‘d’ conversion codes, the packed representation uses the IEEE 754 binary32 (for ‘f’) or binary64 (for ‘d’) format, regardless of the floating-point format used by the platform.

  2. The ‘P’ format character is only available for the native byte ordering (selected as the default or with the ‘@’ byte order character). The byte order character ‘=’ chooses to use little- or big-endian ordering based on the host system. The struct module does not interpret this as native ordering, so the ‘P’ format is not available.

…………………………………………………………………………………………………………………………………………………………………………………………………………
……………………………………分割線………………………………………………………
以下是一個例子:
如一個消息頭

名稱 類型定義 描述
MsgType uInt32 消息類型
seqNum Int64 消息序號
BodyLength uint32 消息體長度

對照上面的表,首先消息是網絡消息,所以fmt第一個字符是!,其次uint32對應L,Int64對應q,所以fmt就是!LqL


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM