0

I need to turn bytes from a file into a string of 1's and 0's,

file = open("Some_File.jpg","rb")
data = file.read()
file.close()
binary = some_function(data)
print(binary)
>>> 0100101000010101001...

I've managed to get something equivalent by converting the bytes into Base64 first, however this makes the size of the string very long. Other questions I have looked at are about turning binary strings into bytes, but I can't find any of the opposite.

This question was marked as a duplicate of another question, however this question is turning a string into binary. If I wanted to do that, I would just convert it into Base64, however it makes it far too long. I need a way to to 'bytes' directly into a string of 1's and 0's

14
  • What do you mean by "makes it far too long"? Are you worried about the time it takes to do this, or the literal length of the resulting binary string? The later will be the same by definition (8 * number_of_bytes characters), regardless of how you convert to binary. Commented Nov 11, 2019 at 9:53
  • 1
    "however this makes the size of the string very long" How long are you expecting the result to be, and why? And why do you think that "converting bytes directly" would produce a shorter result than converting "a string"? And why are you expecting base64 to be helpful here at all? Commented Nov 11, 2019 at 9:54
  • @Aleon Both, really, as the file I'm testing it on is about 4KB, and the result of the file is 49KB, and it takes about a second to go through it Commented Nov 11, 2019 at 9:56
  • @Karl Knechtel I was using Base64 as a way to turn the bytes into some sort of string, and then turning that string into binary Commented Nov 11, 2019 at 9:56
  • I'm using the binary string it produced in an algorithm I'm creating that turns binary data into a sort of 'Base 3.5' format that takes up half of the space of a normal string of 1's and 0's Commented Nov 11, 2019 at 9:59

1 Answer 1

0

Based on the discussion in the comments, I am not convinced that what you ask about is what you actually want to do. However assuming you want to convert a python bytestring to a string of literal zeros and ones, here's one way to do it:

import itertools

def bytes_to_bits(bytes_to_print: bytes):
    bits = [
        ["1" if byte & 2 ** i else "0" for i in range(7, -1, -1)]
        for byte in bytes_to_print
    ]
    return ''.join(itertools.chain.from_iterable(bits))


if __name__ == "__main__":
    print(bytes_to_bits(b"ABC"))
Sign up to request clarification or add additional context in comments.

7 Comments

This was exactly what I needed, if you wouldn't mind, could you tell me how I should have worded the question, if this is what I wanted.
Okay, and... how does the result of this code differ from the result of the code you actually tried?
The binary string is shorter!
@Pomegranates-And-Python difficult to tell you, the question in general suggested that there is a misunderstanding somewhere. Your mention of Base64 for example, leads me to think that you are not entirely familiar with how computers handle data. Your Base64 string is longer, because Base64 "wastes" bits on each byte, just so that the given byte can be represented with printable characters.
I know how computers handle data, I just don't look at python that way, I just think: 'Hey, I need to turn 'x' into 'y', what function does that...? Oh! Base64 makes 'bytes' into a string of letters, and then I turn it into binary!'
|