Ask Your Question

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

asked 2022-08-05 04:44:12 +0100

Luce9801 gravatar image

updated 2022-08-08 10:17:32 +0100

FrédéricC gravatar image

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))]



[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 flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2022-08-08 11:32:41 +0100

rburing gravatar image

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
sage: bin(32)
sage: bin(40)
sage: bin(8)

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')

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'))
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2022-08-05 04:44:12 +0100

Seen: 100 times

Last updated: Aug 08 '22