Python:如何制作结构calcsize标准大小为6的整数?

2024-09-24 20:36:12 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我看一下这里:https://docs.python.org/2/library/struct.html第7.3.2.2节。对于Python类型,格式的整型字符没有6个。我试过“6p”甚至“3H”,但这不起作用,例如:

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

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

我的任务是转换^{} specification document, here中包含的二进制消息

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

^{pr2}$

我得到这个错误:

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

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

更新2

因此,我使用以下建议,即使用6s而不是6p,对于文档第4.6节:净订单不平衡指示符(NOII)消息中的以下消息类型“I”,我确实:

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。。。在


谢谢大家的帮助!在


Tags: 字符串self消息类型message标准格式错误
2条回答

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[]6s
只有在消费者方面需要时,才将它们后置处理成int() 这会延迟延迟延迟和解除阻塞。在

+还可以享受Python7.3.2.1。使用“!”进行BigEndian/network订购时的注意事项掩码前缀


^{pr2}$

在更新2中添加另一个问题

"!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            |       

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,)

相关问题 更多 >