Reputation: 235
I have created my own version of AES (baby version) everything is working correctly however.
Some binary numbers somehow pick up a 'b' within them example: b1b10101
I am not very clued up on how python works with binary conversions but when trying to convert to a decimal using: pepee = int(pepe,2)
. It throws the error mentioned in the title when the string contains 'b'.
I found one other answer for this error on here, however the solution does not work for me. using 'format(pepe,'b')' throws an error for me. I suspect it was written for Python 2.
I need to know, how I can prevent these b's from occurring in my binary strings, or how I can convert them back to the original bit value.
Sample code:
subList2 = ['b1', 'b1', '00', '00']
subStr = b1b10000
subStr = ''.join(subList2)
subDec = int(subStr,2)
Please note I did not intend these b's to appear in the string, they appear during runtime
Upvotes: 0
Views: 8156
Reputation: 235
Ok, I got it working. I had made an athematic error in my code, that was producing minus numbers for binary conversion. which created these 'b' characters in place of the minus numbers. now it is fixed.
Upvotes: 0
Reputation: 561
Have you tried making a smale code snippet to just convert a binary string? Where do you get the binary strings from? If you for example make binary string using bin()
, the string will contain a 'b
' character.
print(bin(10))
# Outputs: 0b1010
But if you use format(int, 'b')
instead, it will not contain the 'b
'.
# Set test to a binary string and print it
test = '101001'
print(test)
# Convert test from binary string to int and print it
test = int(test, 2)
print(test)
# Convert test from int to binary string and print it
test = format(test, 'b')
print(test)
Upvotes: 1