First time here? Check out the FAQ!

Ask Your Question
1

How to identify binary digits as integers?

asked 4 years ago

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

0100100001101001

sage: ascii_to_bin('Hi')[4]

1

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

False

sage:

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

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 4 years ago

tmonteil gravatar image

updated 4 years ago

You can make a conversion:

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

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
1
sage: a.parent()
Integer Ring
Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 4 years ago

Seen: 178 times

Last updated: Jun 02 '20