zoukankan      html  css  js  c++  java
  • python struct

    python的struct是用于将对象变为二进制对象的方法。

    import struct
    
    print(struct.pack('>I',10241024))
    print(struct.unpack('>IH',b'xf0xf0xf0xf0x80x80'))

     顺便附上格式化字符的表

    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.

    译(闲得无聊翻一下)

    格式化字符有如下的意思;从C和Python之间的值的转化必须要明确的指定类型。当使用标准size的时候标准size的column是被packed的值的bytes size(大概意思就是说以被解释的值的字节大小吧)也就是说当使用‘<’ '>' '!'或者'='为格式化字符串的开始的时候。当使用native size,这个被packed的value的size是依赖平台的。(fxxk!)

    FormatC TypePython typeStandard sizeNotes
    x pad byte no value    
    c char bytes of length 1 1  
    b signed char integer 1 (1),(3)
    B unsigned char integer 1 (3)
    ? _Bool bool 1 (1)
    h short integer 2 (3)
    H unsigned short integer 2 (3)
    i int integer 4 (3)
    I unsigned int integer 4 (3)
    l long integer 4 (3)
    L unsigned long integer 4 (3)
    q long long integer 8 (2), (3)
    Q unsigned long long integer 8 (2), (3)
    n ssize_t integer   (4)
    N size_t integer   (4)
    e (7) float 2 (5)
    f float float 4 (5)
    d double float 8 (5)
    s char[] bytes    
    p char[] bytes    
    P void * integer   (6)
    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.(译:?转换码相符合C99中定义的_Bool类型,如果这个C99类型不存在,将会以char类型来模拟。在标准模式下,总是表达为1个字节)

    2. 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.(译:q或者Q转换码仅仅在本地字节序而且仅仅在C编译器支持C的long long类型的时候方可使用,如果是windows,则是__int64类型。在标准模式下是可用的)

    3. 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.(译:当试图使用任何的整数转化码来转换非整型的时候,如果非整形类型有__index__()方法这个方法就会被调用来转换成一个整数(在packing之前))

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

    4. The 'n' and 'N' conversion codes are only available for the native size (selected as the default or with the '@' byte order character). For the standard size, you can use whichever of the other integer formats fits your application.(译:n和N转换码仅仅对native sieze才有效(就是说默认。。。。。我也不知道怎么翻,能心领。。。。或者@byte 排序字符????wtf?)对于标准模式,可以使用任何其他整型格式适用于你的程序)

    5. For the 'f''d' and 'e' conversion codes, the packed representation uses the IEEE 754 binary32, binary64 or binary16 format (for 'f''d' or 'e' respectively), regardless of the floating-point format used by the platform.(译:对于f、d、e转换code,将会使用IEEE754 f=>binary32 d=>binary64或者e=>binary16格式进行pack)不用关注平台使用的浮点格式

    6. 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.(译:P格式化字符仅仅在本地字节序下可用。=字节序以宿主机的字节序为字节序。Struct模块不将=解释为本地字节序,所以P格式符将会无法使用)

    7. The IEEE 754 binary16 “half precision” type was introduced in the 2008 revision of the IEEE 754 standard. It has a sign bit, a 5-bit exponent and 11-bit precision (with 10 bits explicitly stored), and can represent numbers between approximately 6.1e-05 and 6.5e+04 at full precision. This type is not widely supported by C compilers: on a typical machine, an unsigned short can be used for storage, but not for math operations. See the Wikipedia page on the half-precision floating-point format for more information.(译:IEEE754 binary16“半精度”类型在IEEE754标准的2008修订被引入,由1个符号位,一个5位的指数,一个11位的精度(其中10位明确存储,也就是说有一个隐藏位)可以全精度的表达大约6.1e-05到6.5e+04之间的数字。这个类型并不是被所有的C编译器支持,典型的,一个无符号的shourt可以用于存储但是不适合用于数学操作。)

    A format character may be preceded by an integral repeat count. For example, the format string '4h' means exactly the same as 'hhhh'.

    一个格式化字符可以冠以一个整数来表达重复,比如格式化字符串‘4h’事实上和‘hhhh’相同

    Whitespace characters between formats are ignored; a count and its format must not contain whitespace though.

    格式化字符串中的空白字符会被忽略,不能包含空白字符

    For the 's' format character, the count is interpreted as the length of the bytes, not a repeat count like for the other format characters; for example, '10s' means a single 10-byte string, while '10c' means 10 characters. If a count is not given, it defaults to 1. For packing, the string is truncated or padded with null bytes as appropriate to make it fit. For unpacking, the resulting bytes object always has exactly the specified number of bytes. As a special case, '0s' means a single, empty string (while '0c' means 0 characters).

    对于s这个格式化字符,前缀的数字被解释为bytes的长度(注意是bytes不是byte)不像其他的被解释为重复;例如10s意味着“一个”10byte长的string,然而10c意味着10个字符。如果前缀数字没有给,默认就是1.对于packing操作string被阶段或者填充null以确保合适。对于unpacking,结果byes对象总是由明确的byte数。特例,0s代表一个空字符串,然而0c代表0个字符

    When packing a value x using one of the integer formats ('b''B''h''H''i''I''l''L''q''Q'), if x is outside the valid range for that format then struct.error is raised.

    当使用b B h H i I l L q Q对值x进行packing的时候,如果x在允许的格式的有效值之外将会引起一个struct.error异常

    Changed in version 3.1: In 3.0, some of the integer formats wrapped out-of-range values and raised DeprecationWarning instead of struct.error.

    在3.1版本中3.0中一些整型格式包装超过范围值并且发出一个DeprecationWarning而不是跑出struct.error异常

    The 'p' format character encodes a “Pascal string”, meaning a short variable-length string stored in a fixed number of bytes, given by the count. The first byte stored is the length of the string, or 255, whichever is smaller. The bytes of the string follow. If the string passed in to pack() is too long (longer than the count minus 1), only the leadingcount-1 bytes of the string are stored. If the string is shorter than count-1, it is padded with null bytes so that exactly count bytes in all are used. Note that for unpack(), the 'p' format character consumes count bytes, but that the string returned can never contain more than 255 bytes.

    p格式字符,encode一个“Pascal string”,意味着一个短小的变长的字符串会被存储在一个给定长的bytes中。存储的第一个byte是String的长度或者255,以短的为准。然后存接下来的字符串bytes。如果传入pack函数的string过长,只有前缀长度-1的字节被存储。如果string短于前缀长度-1,将会以null bytes进行填充,所以事实上,所有的byte都被使用了。对于unpack函数来说,p格式符消耗前缀count那么多的字符,但是永远不能返回超过255字节的String

    For the '?' format character, the return value is either True or False. When packing, the truth value of the argument object is used. Either 0 or 1 in the native or standard bool representation will be packed, and any non-zero value will be True when unpacking.

    ?格式符,返回的要么是True要么是False,当Packing的时候,0或者1都会被packed,任何非零值都会被当做True

  • 相关阅读:
    NLP Best Practices
    Bert模型精简方法
    delphi:对象的创建(create)与释放(free/destory)(转载)
    delphi:Format格式化函数(转载)
    delphi:destroy, free, freeAndNil, release用法和区别(转载)
    delphi:function ParamStr(i:LongInt):string
    jquery:上传文件
    bootstrapValidator中用ajax校验
    jquery中实时监控文本框的变化
    jquery判断checkbox是否选中
  • 原文地址:https://www.cnblogs.com/sunyuw/p/8386458.html
Copyright © 2011-2022 走看看