# unable to coerce <class 'sage.monoids.string_monoid_element.StringMonoidElement'> to an integer

In the following code snippet,

from sage.crypto.util import ascii_to_bin

arr = [12, 32, 40]

arr2 = [ascii_to_bin(chr(arr[i])) for i in range(0,len(arr))]

print(arr2)

print(type(arr2[0]))

Output:
[00001100, 00100000, 00101000]
<class 'sage.monoids.string_monoid_element.StringMonoidElement'>

What I seek is the conversion of 00001100 to Integer, not like the ASCII from binary but more on the lines of a format that I can use for XORing later. I tried converting to Integer class which proved futile. What I want to do is eventually XOR each bit of the binary representation of each element of arr with something else.

edit retag close merge delete

Sort by » oldest newest most voted

If you want to XOR you can just use integers directly with the ^^ operator in SageMath (or ^ in Python). Integers can be displayed as binary using e.g. the bin function:

sage: 32 ^^ 40
8
sage: bin(32)
'0b100000'
sage: bin(40)
'0b101000'
sage: bin(8)
'0b1000'

If your input is given as a string of ASCII characters then you can turn each character into an integer using the ord function:

sage: ord('A')
65

If you want to turn the output of ascii_to_bin into an integer, use ascii_integer:

sage: from sage.crypto.util import ascii_to_bin, ascii_integer
sage: ascii_integer(ascii_to_bin('A'))
65
more