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
sbmzhcn
V2EX  ›  Python

struct pack 问题,与文档上的结果不一样

  •  
  •   sbmzhcn · 2014-12-20 13:22:28 +08:00 · 2429 次点击
    这是一个创建于 3415 天前的主题,其中的信息可能已经有所发展或是发生改变。
    文档上的例子:
    A basic example of packing/unpacking three integers:

    >>>
    >>> from struct import *
    >>> pack('hhl', 1, 2, 3)
    '\x00\x01\x00\x02\x00\x00\x00\x03'
    >>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
    (1, 2, 3)
    >>> calcsize('hhl')
    8

    我电脑上的实际结果:
    Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> from struct import *
    >>> pack('hhl', 1, 2, 3)
    '\x01\x00\x02\x00\x03\x00\x00\x00'
    >>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
    (256, 512, 50331648)
    >>> calcsize('hhl')
    8
    >>>
    6 条回复    2014-12-20 13:47:52 +08:00
    casparchen
        1
    casparchen  
       2014-12-20 13:35:24 +08:00
    是我眼睛看花了吗, 你pack的结果跟unpack时用的不一致
    savebox
        2
    savebox  
       2014-12-20 13:40:03 +08:00
    bigendian和little endian的问题
    你os是win 例子里面就不一定是win哦
    sbmzhcn
        3
    sbmzhcn  
    OP
       2014-12-20 13:40:18 +08:00
    @casparchen 是不一样,你没看到上面的代码有问题吗, 1 pack的结果应该是 \x00\x01 但实际却是\x01\x00,

    原文档: unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03') 结果是(1, 2, 3)
    我的unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03') 结果是(256, 512, 50331648)
    sbmzhcn
        4
    sbmzhcn  
    OP
       2014-12-20 13:41:27 +08:00
    @savebox 怎么解决,我现在需要 pack('h', 16) 的结果是 \x00\x10 但它的结果总是 \x10\x00
    glasslion
        5
    glasslion  
       2014-12-20 13:45:54 +08:00
    http://zh.wikipedia.org/wiki/%E5%AD%97%E8%8A%82%E5%BA%8F

    By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler).

    pack('>hhl', 1, 2, 3)
    Out[8]: '\x00\x01\x00\x02\x00\x00\x00\x03'

    pack('<hhl', 1, 2, 3)
    Out[9]: '\x01\x00\x02\x00\x03\x00\x00\x00'

    unpack('>hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
    Out[10]: (1, 2, 3)

    unpack('<hhl', '\x01\x00\x02\x00\x03\x00\x00\x00')
    Out[11]: (1, 2, 3)
    savebox
        6
    savebox  
       2014-12-20 13:47:52 +08:00   ❤️ 1
    >>> unpack('<hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
    (256, 512, 50331648)
    >>> unpack('>hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
    (1, 2, 3)

    我都是强制加上>或<, 看手册,>是big.<是little, 不加就是系统默认的endian格式,pack同样
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2978 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:45 · PVG 21:45 · LAX 06:45 · JFK 09:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.