Python:如何生成标准大小为 6 的 struct.calcsize 整数?

Python: how to make a struct.calcsize integer with a standard size 6?

如果我看这里:https://docs.python.org/2/library/struct.html 第 7.3.2.2 节。格式字符,标准大小为 6 的 Python 类型整数没有格式字母。我尝试过“6p”甚至“3H”,但这不起作用,例如:

import struct
struct.calcsize('!6p')

抱歉,如果这听起来很愚蠢...我正在学习如何将字符串转换为打包二进制数据,这是第一次将字符串消息转换为长度为 6 且值为整数的字符串消息。那么得到标准大小 6 的整数的正确字母格式是什么?

更新

我的任务是转换NASDAQ TotalVIEW-ITCH 5.0 specification document, here

中包含的二进制消息

因此,例如,如果您查看 第 4.1 节:系统事件消息(第 3 页),我会将 'S' 类型的消息转换如下:

class SystemEventMessage(Message): 
    type = 'S'
    description = "System Event Message"
    message_size = struct.calcsize("!HH6pc") + 1
def __init__(self, message):
    (self.locate,self.tracking,self.timestamp, self.code) = struct.unpack("!HH6pc", message[1:])

def pack(self):
    return struct.pack("!cHH6pc", self.type,self.locate,self.tracking, self.timestamp, self.code)

我收到这个错误:

struct.error: unpack requires a string argument of length 11

所以我假设我的错误与“6p”有关,因为我找不到任何标准大小为 6 的整数。

更新 2

所以我使用下面的建议使用 6s 而不是 6p,对于文档中的以下消息类型“I” 第 4.6 节:净订单不平衡指标( NOII) 留言, 我做:

class NoiiMessage(ITCH41MarketMessage):
    type = 'I'
    description = "NOII Message"
    message_size = struct.calcsize("!HH6sQQc8sIIIcc") + 1

    def __init__(self, message):
        (self.locate,self.tracking,self.timestamp, self.pairedShares, self.imbalance,
         self.imbalanceDirection, self.stock, self.farPrice, self.nearPrice,
         self.currentRefPrice, self.crossType, self.priceVariationbsindicator
         ) = struct.unpack("!HH6sQQc8sIIIcc", message[1:])

    def pack(self):
        return struct.pack("!cHH6sQQc8sIIIcc", self.type,self.locate,
                           self.tracking, self.timestamp,
                           self.pairedShares, self.imbalance,
                           self.imbalanceDirection, self.stock,
                           self.farPrice, self.nearPrice,
                           self.currentRefPrice, self.crossType,
                           self.priceVariationbsindicator)

我收到这个错误:

struct.error: unpack requires a string argument of length 49

这很奇怪,因为 !HH6sQQc8sIIIcc 的长度是 49...


谢谢大家的帮助!

struct 旨在处理 C 结构。这就是为什么它只有普通的 C 类型。 “6 字节整数”不是普通的 C 类型——你不能写类似 struct s { int6 timestamp; } 的东西来立即获得可用的整数。这就是为什么它在这里也不起作用。

那么,您将如何解决 C 中的问题?你可能会

  • unsigned char ts_data[6];
  • 将值复制到其他地方
  • 填充并
  • 将结果解释为整数

现在,我们所要做的就是在Python中表达同样的东西:

>>> struct.pack('q',1324)
',\x05\x00\x00\x00\x00\x00\x00'    #my arch is big-endian

>>> struct.unpack('q',',\x05\x00\x00\x00\x00')
error: unpack requires a string argument of length 8

>>> struct.unpack('6s',',\x05\x00\x00\x00\x00')
(',\x05\x00\x00\x00\x00',)

>>> s=_[0]
>>> struct.unpack('q',s+'\x00'*2)    #check byte order to find out which side to pad from
(1324,)

NASDAQ TotalView-ITCH 5.0

[S0099123456Q]________________________________ wireline SEQ _____________
 | | |     ||
 | | |     |+---------------[[ Event Code   ]]
 | | |     +----------------[[ Timestamp ns ]]
 | | +----------------------[[ Tracking NUM ]]
 | +------------------------[[ Stock Locate ]]
 +--------------------------[[ Message Type ]]

Name           | Offset | Length | Value   | Notes
---------------|--------|--------|---------|-------------------------------------------------------------
Message Type   | 0      | 1      | “S”     | System Event Message.
Stock Locate   | 1      | 2      | Integer | == 0 Always
Tracking Number| 3      | 2      | Integer | NASDAQ OMX internal tracking number
Timestamp      | 5      | 6      | Integer | Nanoseconds since midnight.
Event Code     |11      | 1      | Alpha   | == { 0 | S | Q | M | E | C } See System Event Codes below.

为了得到NASDAQ_Timestamp,
在面具中使用 6Bchar[] 抓取 6-uchar-s 6s
并将它们后处理成 int() 只有在消费者方面需要时
延迟延迟解锁取消流引擎有线性能。

+ 也享受 Python 7.3.2.1。使用“!”时关于 BigEndian / 网络排序的说明掩码前缀


>>> struct.pack(   ">c2H6sc", "S", 0, 99, "123456", "Q" )
'S\x00\x00\x00c123456Q'

>>> struct.unpack( ">c2H6sc", "S\x00\x00\x00c123456Q" )
('S', 0, 99, '123456', 'Q')
  |   |   |        |    |
  |   |   |        |    +---------------[[ Event Code   ]]
  |   |   |        +--------------------[[ Timestamp ns ]]
  |   |   +-----------------------------[[ Tracking NUM ]]
  |   +---------------------------------[[ Stock Locate ]]
  +-------------------------------------[[ Message Type ]]

UPDATE2

中补充另一个问题
"!HH6sQQc8sIIIcc"                              _
 +||-||||-||||||----------------------------1 |_|_ "!"   a "Network"-Byte-order
  +|-||||-||||||----------------------------2 |_|   H as a 2 Byte unsigned short
   | |||| ||||||                            3 |_|_ 
   +-||||-||||||----------------------------4-|_|   H as a 2 Byte unsigned short                                
     |||| ||||||                            5 |_|_ 
     +|||-||||||----------------------------6-|_|  6s as a 6 Byte char[]
      ||| ||||||                            7 |_|  
      ||| ||||||                            8 |_|  
      ||| ||||||                            9 |_|  
      ||| ||||||                           10 |_|  
      ||| ||||||                            1 |_|_ 
      +||-||||||--------------------------- 2-|_|   Q as a 8 Byte unsigned long long
       || ||||||                            3 |_|  
       || ||||||                            4 |_|  
       || ||||||                            5 |_|  
       || ||||||                            6 |_|  
       || ||||||                            7 |_|  
       || ||||||                            8 |_|  
       || ||||||                            9 |_|_ 
       +|-||||||---------------------------20-|_|   Q as a 8 Byte unsigned long long
        | ||||||                            1 |_|  
        | ||||||                            2 |_|  
        | ||||||                            3 |_|  
        | ||||||                            4 |_|  
        | ||||||                            5 |_|  
        | ||||||                            6 |_|  
        | ||||||                            7 |_|_ 
        +-||||||----------------------------8-|_|_  c as a 1 Byte char
          +|||||----------------------------9-|_|  8s as a 8 Byte char[]
           |||||                           30 |_|  
           |||||                            1 |_|  
           |||||                            2 |_|  
           |||||                            3 |_|  
           |||||                            4 |_|  
           |||||                            5 |_|  
           |||||                            6 |_|_ 
           +||||----------------------------7-|_|   I as a 4 Byte unsigned int
            ||||                            8 |_|                                     
            ||||                            9 |_|                                     
            ||||                           40 |_|_ 
            +|||----------------------------1-|_|   I as a 4 Byte unsigned int
             |||                            2 |_|                             
             |||                            3 |_|                             
             |||                            4 |_|_ 
             +||----------------------------5-|_|   I as a 4 Byte unsigned int
              ||                            6 |_|                             
              ||                            7 |_|                             
              ||                            8 |_|_ 
              +|----------------------------9-|_|_  c as a 1 Byte char
               +---------------------------50-|_|_  c as a 1 Byte char

其中:

Format  | C-type               | Python-type        | Standard size
========|======================|====================|===============
     x  | pad byte             | no value           |     
     c  | char                 | string of length 1 | 1 
     b  | signed char          | integer            | 1 
     B  | unsigned char        | integer            | 1 
     ?  | _Bool                | bool               | 1 
     h  | short                | integer            | 2 
     H  | unsigned short       | integer            | 2 
     i  | int                  | integer            | 4 
     I  | unsigned int         | integer            | 4 
     l  | long                 | integer            | 4 
     L  | unsigned long        | integer            | 4 
     q  | long long            | integer            | 8 
     Q  | unsigned long long   | integer            | 8 
     f  | float                | float              | 4 
     d  | double               | float              | 8 
     s  | char[]               | string             | 
     p  | char[]               | string             |  
     P  | void *               | integer            |