欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

struct – Working with Binary Data?

系統(tǒng) 1903 0

struct – Working with Binary Data - Python Module of the Week

struct – Working with Binary Data ?

Purpose: Convert between strings and binary data.
Available In: 1.4 and later

The struct module includes functions for converting between strings of bytes and native Python data types such as numbers and strings.

Functions vs. Struct Class ?

There are a set of module-level functions for working with structured values, and there is also the Struct class (new in Python 2.5). Format specifiers are converted from their string format to a compiled representation, similar to the way regular expressions are. The conversion takes some resources, so it is typically more efficient to do it once when creating a Struct instance and call methods on the instance instead of using the module-level functions. All of the examples below use the Struct class.

Packing and Unpacking ?

Structs support packing data into strings, and unpacking data from strings using format specifiers made up of characters representing the type of the data and optional count and endian-ness indicators. For complete details, refer to the standard library documentation .

In this example, the format specifier calls for an integer or long value, a two character string, and a floating point number. The spaces between the format specifiers are included here for clarity, and are ignored when the format is compiled.

            
              import
            
            
              struct
            
            
              import
            
            
              binascii
            
            
              values
            
            
              =
            
            
              (
            
            
              1
            
            
              ,
            
            
              'ab'
            
            
              ,
            
            
              2.7
            
            
              )
            
            
              s
            
            
              =
            
            
              struct
            
            
              .
            
            
              Struct
            
            
              (
            
            
              'I 2s f'
            
            
              )
            
            
              packed_data
            
            
              =
            
            
              s
            
            
              .
            
            
              pack
            
            
              (
            
            
              *
            
            
              values
            
            
              )
            
            
              print
            
            
              'Original values:'
            
            
              ,
            
            
              values
            
            
              print
            
            
              'Format string  :'
            
            
              ,
            
            
              s
            
            
              .
            
            
              format
            
            
              print
            
            
              'Uses           :'
            
            
              ,
            
            
              s
            
            
              .
            
            
              size
            
            
              ,
            
            
              'bytes'
            
            
              print
            
            
              'Packed Value   :'
            
            
              ,
            
            
              binascii
            
            
              .
            
            
              hexlify
            
            
              (
            
            
              packed_data
            
            
              )
            
          

The example converts the packed value to a sequence of hex bytes for printing with binascii.hexlify() , since some of the characters are nulls.

          $ python struct_pack.py

Original values: (1, 'ab', 2.7)
Format string  : I 2s f
Uses           : 12 bytes
Packed Value   : 0100000061620000cdcc2c40
        

If we pass the packed value to unpack() , we get basically the same values back (note the discrepancy in the floating point value).

            
              import
            
            
              struct
            
            
              import
            
            
              binascii
            
            
              packed_data
            
            
              =
            
            
              binascii
            
            
              .
            
            
              unhexlify
            
            
              (
            
            
              '0100000061620000cdcc2c40'
            
            
              )
            
            
              s
            
            
              =
            
            
              struct
            
            
              .
            
            
              Struct
            
            
              (
            
            
              'I 2s f'
            
            
              )
            
            
              unpacked_data
            
            
              =
            
            
              s
            
            
              .
            
            
              unpack
            
            
              (
            
            
              packed_data
            
            
              )
            
            
              print
            
            
              'Unpacked Values:'
            
            
              ,
            
            
              unpacked_data
            
          
          $ python struct_unpack.py

Unpacked Values: (1, 'ab', 2.700000047683716)
        

Endianness ?

By default values are encoded using the native C library notion of “endianness”. It is easy to override that choice by providing an explicit endianness directive in the format string.

            
              import
            
            
              struct
            
            
              import
            
            
              binascii
            
            
              values
            
            
              =
            
            
              (
            
            
              1
            
            
              ,
            
            
              'ab'
            
            
              ,
            
            
              2.7
            
            
              )
            
            
              print
            
            
              'Original values:'
            
            
              ,
            
            
              values
            
            
              endianness
            
            
              =
            
            
              [
            
            
              (
            
            
              '@'
            
            
              ,
            
            
              'native, native'
            
            
              ),
            
            
              (
            
            
              '='
            
            
              ,
            
            
              'native, standard'
            
            
              ),
            
            
              (
            
            
              '<'
            
            
              ,
            
            
              'little-endian'
            
            
              ),
            
            
              (
            
            
              '>'
            
            
              ,
            
            
              'big-endian'
            
            
              ),
            
            
              (
            
            
              '!'
            
            
              ,
            
            
              'network'
            
            
              ),
            
            
              ]
            
            
              for
            
            
              code
            
            
              ,
            
            
              name
            
            
              in
            
            
              endianness
            
            
              :
            
            
              s
            
            
              =
            
            
              struct
            
            
              .
            
            
              Struct
            
            
              (
            
            
              code
            
            
              +
            
            
              ' I 2s f'
            
            
              )
            
            
              packed_data
            
            
              =
            
            
              s
            
            
              .
            
            
              pack
            
            
              (
            
            
              *
            
            
              values
            
            
              )
            
            
              print
            
            
              print
            
            
              'Format string  :'
            
            
              ,
            
            
              s
            
            
              .
            
            
              format
            
            
              ,
            
            
              'for'
            
            
              ,
            
            
              name
            
            
              print
            
            
              'Uses           :'
            
            
              ,
            
            
              s
            
            
              .
            
            
              size
            
            
              ,
            
            
              'bytes'
            
            
              print
            
            
              'Packed Value   :'
            
            
              ,
            
            
              binascii
            
            
              .
            
            
              hexlify
            
            
              (
            
            
              packed_data
            
            
              )
            
            
              print
            
            
              'Unpacked Value :'
            
            
              ,
            
            
              s
            
            
              .
            
            
              unpack
            
            
              (
            
            
              packed_data
            
            
              )
            
          
          $ python struct_endianness.py

Original values: (1, 'ab', 2.7)

Format string  : @ I 2s f for native, native
Uses           : 12 bytes
Packed Value   : 0100000061620000cdcc2c40
Unpacked Value : (1, 'ab', 2.700000047683716)

Format string  : = I 2s f for native, standard
Uses           : 10 bytes
Packed Value   : 010000006162cdcc2c40
Unpacked Value : (1, 'ab', 2.700000047683716)

Format string  : < I 2s f for little-endian
Uses           : 10 bytes
Packed Value   : 010000006162cdcc2c40
Unpacked Value : (1, 'ab', 2.700000047683716)

Format string  : > I 2s f for big-endian
Uses           : 10 bytes
Packed Value   : 000000016162402ccccd
Unpacked Value : (1, 'ab', 2.700000047683716)

Format string  : ! I 2s f for network
Uses           : 10 bytes
Packed Value   : 000000016162402ccccd
Unpacked Value : (1, 'ab', 2.700000047683716)
        

Buffers ?

Working with binary packed data is typically reserved for highly performance sensitive situations or passing data into and out of extension modules. In such situations, you can optimize by avoiding the overhead of allocating a new buffer for each packed structure. The pack_into() and unpack_from() methods support writing to pre-allocated buffers directly.

            
              import
            
            
              struct
            
            
              import
            
            
              binascii
            
            
              s
            
            
              =
            
            
              struct
            
            
              .
            
            
              Struct
            
            
              (
            
            
              'I 2s f'
            
            
              )
            
            
              values
            
            
              =
            
            
              (
            
            
              1
            
            
              ,
            
            
              'ab'
            
            
              ,
            
            
              2.7
            
            
              )
            
            
              print
            
            
              'Original:'
            
            
              ,
            
            
              values
            
            
              print
            
            
              print
            
            
              'ctypes string buffer'
            
            
              import
            
            
              ctypes
            
            
              b
            
            
              =
            
            
              ctypes
            
            
              .
            
            
              create_string_buffer
            
            
              (
            
            
              s
            
            
              .
            
            
              size
            
            
              )
            
            
              print
            
            
              'Before  :'
            
            
              ,
            
            
              binascii
            
            
              .
            
            
              hexlify
            
            
              (
            
            
              b
            
            
              .
            
            
              raw
            
            
              )
            
            
              s
            
            
              .
            
            
              pack_into
            
            
              (
            
            
              b
            
            
              ,
            
            
              0
            
            
              ,
            
            
              *
            
            
              values
            
            
              )
            
            
              print
            
            
              'After   :'
            
            
              ,
            
            
              binascii
            
            
              .
            
            
              hexlify
            
            
              (
            
            
              b
            
            
              .
            
            
              raw
            
            
              )
            
            
              print
            
            
              'Unpacked:'
            
            
              ,
            
            
              s
            
            
              .
            
            
              unpack_from
            
            
              (
            
            
              b
            
            
              ,
            
            
              0
            
            
              )
            
            
              print
            
            
              print
            
            
              'array'
            
            
              import
            
            
              array
            
            
              a
            
            
              =
            
            
              array
            
            
              .
            
            
              array
            
            
              (
            
            
              'c'
            
            
              ,
            
            
              '
            
            
              \0
            
            
              '
            
            
              *
            
            
              s
            
            
              .
            
            
              size
            
            
              )
            
            
              print
            
            
              'Before  :'
            
            
              ,
            
            
              binascii
            
            
              .
            
            
              hexlify
            
            
              (
            
            
              a
            
            
              )
            
            
              s
            
            
              .
            
            
              pack_into
            
            
              (
            
            
              a
            
            
              ,
            
            
              0
            
            
              ,
            
            
              *
            
            
              values
            
            
              )
            
            
              print
            
            
              'After   :'
            
            
              ,
            
            
              binascii
            
            
              .
            
            
              hexlify
            
            
              (
            
            
              a
            
            
              )
            
            
              print
            
            
              'Unpacked:'
            
            
              ,
            
            
              s
            
            
              .
            
            
              unpack_from
            
            
              (
            
            
              a
            
            
              ,
            
            
              0
            
            
              )
            
          

The size attribute of the Struct tells us how big the buffer needs to be.

          $ python struct_buffers.py

Original: (1, 'ab', 2.7)

ctypes string buffer
Before  : 000000000000000000000000
After   : 0100000061620000cdcc2c40
Unpacked: (1, 'ab', 2.700000047683716)

array
Before  : 000000000000000000000000
After   : 0100000061620000cdcc2c40
Unpacked: (1, 'ab', 2.700000047683716)
        

struct – Working with Binary Data?


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 亚洲欧美日韩在线中文一 | 中文字幕在亚洲第一在线 | 草草影院浮力 | 亚洲AV在线无码播放毛片浪潮 | 99国产在线视频有精品视频 | aaa毛片免费观看 | 国产亚洲欧美另类第一页 | 欧美高清免费 | 国产在线观看午夜不卡 | 色午夜日本 | 国产亚洲精品久久久久婷婷图片 | 久久国产精品久久久久久久久久 | 日韩中文字幕av | 久草日韩| 青青免费视频精品一区二区 | 久久新网址 | 丝袜美腿一区 | 国产精品欧美亚洲日本综合 | 国产精品久久久久久久久久红粉 | 一级做a爰性色毛片免费 | 日韩精品第二页 | 亚洲激情小视频 | 国产精品视频网 | 亚洲精品乱码8久久久久久日本 | 欧美高清一区二区三区欧美 | 久久人人爽人人爽人人 | 亚洲精品久久婷婷丁香51 | 欧美人成网站 | 比比资源先锋影音网 | 久久香蕉国产线看观看网站 | 4hu44四虎在线观看 | 亚洲区第一页 | 毛片基地免费视频a | 欧美高清hd | 国产精品毛片一区二区三区 | 成人免费影 | 国产福利不卡视频在免费播放 | 2021国产在线视频 | 天堂中文资源在线观看 | 成人年鲁鲁在线观看视频 | 九热精品 |