Python Forum

Full Version: How to Read Binary Data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I have a file that contains binary data such as "0100100001100101011011000110110001101111000000010000001000000011".

So far I have written the following piece of code:
with open('binary_file.txt', 'r') as open_file:
    data = open_file.read(8)

    while data != '':
        print(data, end="")
        data = open_file.read(8)
It reads 8 characters at a time from the file. But I don't know how to convert it into plaintext English.
What do you want to do? Converting the binary 010010 in 8 bit chunks?

# example with encoding
result = []
with open('binary_file.txt') as open_file:
    # loop vorever
    while True:
        # read 8 bytes, return value is a str
        data = open_file.read(8)
        # if a str, bytes or some other sequence or
        # container is empty, it's boolean is False
        if not data:
            # break out of the loop
            break
        # convert binary (0101010011) into
        # a int
        value = int(data, 2)
        # append the value to the list
        result.append(value)
# example without encoding
# resulting object of read method is bytes
result = []
with open('binary_file.txt', 'rb') as open_file:
    # loop vorever
    while True:
        # read 8 bytes, return value is bytes
        data = open_file.read(8)
        # if a str, bytes or some other sequence or
        # container is empty, it's boolean is False
        if not data:
            # break out of the loop
            break
        # convert binary (0101010011) into
        # a int. bytes can also used as input, but
        # it must be a valid value for the required
        # conversion. In this case only 0 and 1 is allowed
        # 0 = 0x30 and 1 = 0x31
        value = int(data, 2)
        # append the value to the list
        result.append(value)
Usually values like integers, floats or complex are stored in binary form using the maximum possible of one or more bytes.
In your case one byte counts as one bit.

Quote:Init signature: int(self, /, *args, **kwargs)
Docstring:
int([x]) -> integer
int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.__int__(). For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base. The literal can be preceded by '+' or '-' and be surrounded
by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
4
Type: type
Subclasses: bool, IntEnum, IntFlag, _NamedIntConstant