Python 十六进制比较

发布于 2024-08-14 07:15:14 字数 308 浏览 2 评论 0原文

我遇到了一个问题,希望有人能帮我解决!

我有一个十六进制数字 = '0x00000000' 的字符串,这意味着:

0x01000000 = apple  
0x00010000 = orange  
0x00000100 = banana   

与这些的所有组合都是可能的。即,0x01010000 = 苹果 & Orange

我如何从我的字符串中确定它是什么水果?我用所有组合制作了一本字典,然后与它进行比较,它起作用了!但我想知道一种更好的方法。

I got a problem I was hoping someone could help me figure out!

I have a string with a hexadecimal number = '0x00000000' which means:

0x01000000 = apple  
0x00010000 = orange  
0x00000100 = banana   

All combinations with those are possible. i.e., 0x01010000 = apple & orange

How can I from my string determine what fruit it is? I made a dictionary with all the combinations and then comparing to that, and it works! But I am wondering about a nicer way of doing it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月苍老的讽刺 2024-08-21 07:15:14

通过使用 int() 内置函数并指定基数,将字符串转换为整数:

>>> int('0x01010000',16)
16842752

现在,您有了一个表示位集的标准整数。使用 &| 和任何其他按位运算符来测试各个位。

>>> value  = int('0x01010000',16)
>>> apple  = 0x01000000
>>> orange = 0x00010000
>>> banana = 0x00000100
>>> bool(value & apple) # tests if apple is part of the value
True
>>> value |= banana     # adds the banana flag to the value
>>> value &= ~orange    # removes the orange flag from the value

现在,如果您需要转换回字符串:

>>> hex(value)
'0x1000100'

Convert your string to an integer, by using the int() built-in function and specifying a base:

>>> int('0x01010000',16)
16842752

Now, you have a standard integer representing a bitset. use &, | and any other bitwise operator to test individual bits.

>>> value  = int('0x01010000',16)
>>> apple  = 0x01000000
>>> orange = 0x00010000
>>> banana = 0x00000100
>>> bool(value & apple) # tests if apple is part of the value
True
>>> value |= banana     # adds the banana flag to the value
>>> value &= ~orange    # removes the orange flag from the value

Now, if you need to convert back to your string:

>>> hex(value)
'0x1000100'
苍白女子 2024-08-21 07:15:14

您可以首先将字符串转换为整数:

s = "0x01010000"
i = int(s, 16) #i = 269484032

然后,您可以为水果设置一个列表:

fruits = [(0x01000000, "apple"), (0x00010000, "orange"), (0x00000100, "banana")]

用于确定您有哪些水果就足够了:

s = "0x01010000"
i = int(s, 16)
for fid,fname in fruits:
    if i&fid>0:
        print "The fruit '%s' is contained in '%s'" % (fname, s)

这里的输出是:

The fruit 'apple' is contained in '0x01010000'
The fruit 'orange' is contained in '0x01010000'

You could first of all convert your string to an integer:

s = "0x01010000"
i = int(s, 16) #i = 269484032

then, you could set up a list for the fruits:

fruits = [(0x01000000, "apple"), (0x00010000, "orange"), (0x00000100, "banana")]

for determing what fruits you have that is enough:

s = "0x01010000"
i = int(s, 16)
for fid,fname in fruits:
    if i&fid>0:
        print "The fruit '%s' is contained in '%s'" % (fname, s)

The output here is:

The fruit 'apple' is contained in '0x01010000'
The fruit 'orange' is contained in '0x01010000'
伴梦长久 2024-08-21 07:15:14
def WhichFruit(n):
    if n & int('0x01000000',16):
        print 'apple'
    if n & int('0x00010000',16):
        print 'orange'
    if n & int('0x00000100',16):
        print 'banana'

WhichFruit(int('0x01010000',16))
def WhichFruit(n):
    if n & int('0x01000000',16):
        print 'apple'
    if n & int('0x00010000',16):
        print 'orange'
    if n & int('0x00000100',16):
        print 'banana'

WhichFruit(int('0x01010000',16))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文