reorder byte order in hex string (python)

后端 未结 4 982
再見小時候
再見小時候 2021-01-05 05:17

I want to build a small formatter in python giving me back the numeric values embedded in lines of hex strings.

It is a central part of my formatter and should be re

4条回答
  •  花落未央
    2021-01-05 06:02

    array.arrays have a byteswap method:

    import binascii
    import struct
    import array
    x = binascii.unhexlify('b62e000052e366667a66408d')
    y = array.array('h', x)  
    y.byteswap()
    s = struct.Struct('

    The h in array.array('h', x) was chosen because it tells array.array to regard the data in x as an array of 2-byte shorts. The important thing is that each item be regarded as being 2-bytes long. H, which signifies 2-byte unsigned short, works just as well.

提交回复
热议问题