Python - Decimal to Hex, Reverse byte order, Hex to Decimal

后端 未结 5 2222
误落风尘
误落风尘 2021-01-01 06:51

I\'ve been reading up a lot on stuct.pack and hex and the like.

I am trying to convert a decimal to hexidecimal with 2-bytes. Reverse the hex bit order, then convert

5条回答
  •  爱一瞬间的悲伤
    2021-01-01 07:10

    Keep in mind that 'hex'(base 16 0-9 and a-f) and 'decimal'(0-9) are just constructs for humans to represent numbers. It's all bits to the machine.

    The python hex(int) function produces a hex 'string' . If you want to convert it back to decimal:

    >>> x = 36895
    >>> s = hex(x)
    >>> s
    '0x901f'
    >>> int(s, 16)  # interpret s as a base-16 number
    

提交回复
热议问题