推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
wave4yy
V2EX  ›  Python

关于 ctypes 中 Array 的问题

  •  
  •   wave4yy · Dec 11, 2019 · 3149 views
    This topic created in 2482 days ago, the information mentioned may be changed or developed.

    请教一下大家

    from ctypes import *
    
    raw_bytes = b"\x00\x01\x00"
    class Test(Structure):
        _fields_ = [
            ("name1", c_char * 2),
            ("name2", c_char)
        ]
    
    buf = create_string_buffer(len(raw_bytes))
    buf.raw = raw_bytes
    test = Test.from_buffer(buf)
    print(test.name1)
    print(test.name2)
    

    输出为

    b''
    b'\x00'
    

    想将一串 bytes 数据直接转为结构体,试了一下上面的写法,发现存在一个问题,类型为 c_char_Array_2 的 name1 变量的值为空,反而 c_char 类型的 name2 变量的值为\x00,猜测会不会是字符串的\x00 截断的问题,希望能够得到解决

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   800 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 20:55 · PVG 04:55 · LAX 13:55 · JFK 16:55
    ♥ Do have faith in what you're doing.