3

Assuming I have some ASCII characters in a string, let's say s = ABC, how can I retrieve the binary representation as a string?

In this case,

A = '01000001'
B = '01000010'
C = '01000011'

so I want something like make_binary('ABC') to return '010000010100001001000011'

I know I can get the hex values for a string. I know I can get the binary representation of an integer. I don't know if there's any way to tie all these pieces together.

2
  • 1
    The ord() function gets the numeric encoding of a character. Get the bindary representation of that integer, and the concatenate them. Commented May 7, 2019 at 20:04
  • Use ord() as @Barmar wrote and then use bin() on the result. And add some polishing ;) Commented May 7, 2019 at 20:06

2 Answers 2

5

Use the ord() funcction to get the integer encoding of each character.

def make_binary(s):
    return "".join([format(ord(c), '08b') for c in s])
print(make_binary("ABC"))

08b formatting returns the number formatted as 8 bits with leading zeroes.

Sign up to request clarification or add additional context in comments.

1 Comment

I put a new answer, because I think you miss-handle characters above 127 (and in such case, you should decide the endianess).
3

I think the other answer is wrong. Maybe I interpret wrongly the question.

In any case, I think you are asking for the 'bit' representation. Binary often is used for bytes representation (the .bin files, etc.)

The byte representation is given by an encoding, so you should encode the string, and you will get a byte array. This is your binary (as byte) representation.

But it seems you are asking 'bit-representation'. That is different (and the other answer, IMHO is wrong). You may convert the byte array into bit representation, like on the other answer. Note: you are converting bytes. The other answer will fails on any characters above 127, by showing you only the binary representation of one byte.

So:

def make_binary(s):
    return "".join(format(c, '08b') for c in s.encode('utf-8'))

and the test (which file on @Barmar answer).

>>> print(make_binary("ABC"))
010000010100001001000011
>>> print(make_binary("Á"))
1100001110000001

1 Comment

The question does state the string is ASCII, so values larger than 127 cannot occur. This solution is for the general case with utf-8, very nice !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.