V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2019-12-11 12:22:46 +08:00 · 2438 次点击
    这是一个创建于 1591 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请教一下大家

    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 截断的问题,希望能够得到解决

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2826 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 11:55 · PVG 19:55 · LAX 04:55 · JFK 07:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.