十六进制字符串在Python 3.2中签署int? [英] Hex string to signed int in Python 3.2?

查看:204
本文介绍了十六进制字符串在Python 3.2中签署int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Python 3.2中将十六进制字符串转换为带符号的int?



我能想到的最好方法是

pre $ h ='9DA92DAB'
b = bytes(h,'utf-8')
ba = binascii.a2b_hex(b)
print(int.from_bytes(ba,byteorder ='big',signed = True))

有一个更简单的方法?无符号是如此容易:int(h,16)



顺便说一句,问题的由来是 itunes persistent id - 音乐库xml版本和iTunes十六进制版本

解决方案

  x = int(h,16)
if x> 0x7FFFFFFF:
x - = 0x100000000


How do I convert a hex string to a signed int in Python 3.2?

The best I can come up with is

h = '9DA92DAB'
b = bytes(h, 'utf-8')
ba = binascii.a2b_hex(b)
print(int.from_bytes(ba, byteorder='big', signed=True))

Is there a simpler way? Unsigned is so much easier: int(h, 16)

BTW, the origin of the question is itunes persistent id - music library xml version and iTunes hex version

解决方案

x = int(h,16)
if x > 0x7FFFFFFF:
    x -= 0x100000000

这篇关于十六进制字符串在Python 3.2中签署int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆