当前位置: 首页>>代码示例>>Python>>正文


Python BitStream.pos方法代码示例

本文整理汇总了Python中bitstring.BitStream.pos方法的典型用法代码示例。如果您正苦于以下问题:Python BitStream.pos方法的具体用法?Python BitStream.pos怎么用?Python BitStream.pos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bitstring.BitStream的用法示例。


在下文中一共展示了BitStream.pos方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from bitstring import BitStream [as 别名]
# 或者: from bitstring.BitStream import pos [as 别名]
def main(argv):
    if len(argv) < 4:
        print("Usage: " + argv[0] + " <header dump> <type (hex|bin|raw)> <definition>")
        return
    
    filename = argv[1]
    typ = argv[2]
    definition = []
    for argc in range(3, len(argv)):
        definition.append(argv[argc])

    print("File: " + filename)
    print("Definition: " + ", ".join(definition))
    print("Type: " + typ)
    
    bits = []
    for d in definition:
        bits = parseDefinition(d, bits)
    
    content = None
    field = None
    with open(filename) as f:
            if typ == "hex":
                content = f.read().strip()
                field = BitStream("0x" + content)
                
            if typ == "bin":
                content = f.read().strip()
                field = BitStream("0b" + content)
        
            if typ == "raw":
                field = BitStream(f)
        
    parsed = {}
    pos = 0
    last_header = None
    for bit in bits:
        field.pos = pos
        
        bitlen = 0
        lenexpr = bit.getBits()
        condexpr = bit.getCondition()
        for p in sorted(parsed.keys(), key=len, reverse=True):
            lenexpr = lenexpr.replace(p, str(parsed[p]))
            condexpr = condexpr.replace(p, str(parsed[p]))
        try:
            bitlen = eval(lenexpr)
        except:
            print("Error: " + str(bit.getBits()) + " is not a valid size!")
            return
        try:
            cond = eval(condexpr)
        except:
            print("Error: " + str(bit.getCondition()) + " is not a valid condition! (Parsed to " + condexpr + ")")
            return
        
        if cond:
            try:
                val = field.read(bitlen)
            except:
                print("Could not read field " + str(bit.getName()) + " with " + str(bitlen) + " bits!")
                continue
            
            parsed[bit.getName()] = val.uint
            pos += bitlen
            if last_header is not bit.getPrefix():
                last_header = bit.getPrefix()
                print("")
            
            out = bit.getName() + ": "
            for t, endian in bit.getTypes():
                if t == "hex":
                    out += "0x" + val.hex + " "
                if t == "int":
                    out += str(endianness(val.uint, endian, int(bitlen))) + " "
                if t == "bin":
                    out += "0b" + val.bin + " "
                if t == "str":
                    s = ""
                    for si in range(0, len(str(val)[2:]), 2):
                        out += str(chr(int(str(val)[si + 2:si + 4], 16)))
            print(out)
开发者ID:misc0110,项目名称:UniversalHeaderDecoder,代码行数:84,代码来源:hdec.py


注:本文中的bitstring.BitStream.pos方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。