简体   繁体   English

将字符串转换为二进制然后异或二进制

[英]Converting string to binary then xor binary

So I am trying to convert a string to binary then xor the binary by using the following methods 所以我试图通过使用以下方法将字符串转换为二进制然后二进制

def string_to_binary(s):
    return ' '.join(map(bin,bytearray(s,encoding='utf-8')))

def xor_bin(a,b):
    return int(a,2) ^ int(b,2)

When I try and run the xor_bin function I get the following error: 当我尝试运行xor_bin函数时,出现以下错误:

Exception has occurred: exceptions.ValueError
invalid literal for int() with base 2: '0b1100010 0b1111001 0b1100101 0b1100101 0b1100101'

I can't see what's wrong here. 我看不到这里有什么问题。

bin is bad here; bin在这里不好 it doesn't pad out to eight digits (so you'll lose data alignment whenever the high bit is a 0 and misinterpret all bits to the left of that loss as being lower magnitude than they should be), and it adds a 0b prefix that you don't want. 它不会填充到八位数字(因此,每当高位为0时,您将丢失数据对齐,并且将该损失左侧的所有位误解为幅度小于应有的幅度),并且添加了0b前缀你不想要的 str.format can fix both issues, by zero padding and omitting the 0b prefix (I also removed the space in the joiner string, since you don't want spaces in the result): str.format可以通过零填充并省略0b前缀来解决这两个问题(我也删除了连接符字符串中的空格,因为您不希望在结果中留空格):

def string_to_binary(s):
    return ''.join(map('{:08b}'.format, bytearray(s, encoding='utf-8')))

With that, string_to_binary('byeee') gets you '0110001001111001011001010110010101100101' which is what you want, as opposed to '0b1100010 0b1111001 0b1100101 0b1100101 0b1100101' which is obviously not a (single) valid base-2 integer. 有了这个, string_to_binary('byeee')得到的就是你想要的'0110001001111001011001010110010101100101' ,而'0b1100010 0b1111001 0b1100101 0b1100101 0b1100101'显然不是一个有效的以2为底的整数。

Your question is unclear because you don't show how the two functions you defined where being used when the error occurred — therefore this answer is a guess. 您的问题尚不清楚,因为您没有显示错误发生时在何处定义了两个函数的用法,因此,此答案仅是猜测。

You can convert a binary string representation of an integer into a Python int , (which are stored internally as binary values) by simply using passing it to the int() function — as you're doing in the xor_bin() function. 您可以简单地使用将int()传递给int()函数的方式,将整数的二进制字符串表示形式转换为Python int (内部存储为二进制值),就像在xor_bin()函数中所做的xor_bin() Once you have two int values, you can xor them "in binary" by simply using the ^ operator — which again, you seem to know. 一旦有了两个int值,就可以简单地使用^运算符对它们进行“二进制”运算,这似乎又是您知道的。

This means means to xor the binary string representations of two integers and convert the result back into a binary string representation could be done like this you one of your functions just as it is. 这意味着将两个整数的二进制字符串表示形式进行异或运算,然后将结果转换回二进制字符串表示形式,就可以像这样完成您的函数之一。 Here's what I mean: 这就是我的意思:

def xor_bin(a, b):
    return int(a, 2) ^ int(b, 2)


s1 = '0b11000101111001110010111001011100101'
s2 = '0b00000000000000000000000000001111111'
#    ---------------------------------------
#    '0b11000101111001110010111001010011010'  expected result of xoring them

result = xor_bin(s1, s2)
print bin(result)  # -> 0b11000101111001110010111001010011010

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM