Ask Your Question

How to identify binary digits as integers?

asked 2020-06-02 03:18:35 -0600

akly gravatar image

I would like to know how to recognize the ASCII binary codes of a string as integers values. Look at the following example.

sage: from sage.crypto.util import ascii_to_bin

sage: ascii_to_bin('Hi')


sage: ascii_to_bin('Hi')[4]


sage: ascii_to_bin('Hi')[4] in ZZ



I would be thankful if I get some tips on how to identify the binary digits as integers.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2020-06-02 06:23:28 -0600

tmonteil gravatar image

updated 2020-06-02 07:40:52 -0600

You can make a conversion:

sage: b = ascii_to_bin('Hi')[4]
sage: b

Unfortunately, there is no direct conversion to ZZ:

sage: ZZ(b)
TypeError: unable to coerce <class 'sage.monoids.string_monoid_element.StringMonoidElement'> to an integer

but you can first make b a string an then an integer:

sage: a = ZZ(str(b)) ; a
sage: a.parent()
Integer Ring
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


Asked: 2020-06-02 03:18:35 -0600

Seen: 39 times

Last updated: Jun 02 '20