Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Read Binary Data
#1
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.
Reply
#2
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 410 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  How do I read and write a binary file in Python? blackears 6 6,645 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Correctly read a malformed CSV file data klllmmm 2 1,963 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  Read nested data from JSON - Getting an error marlonbown 5 1,374 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Read data via bluetooth frohr 9 3,402 Jul-10-2022, 09:51 AM
Last Post: frohr
  Write and read back data Aggie64 6 1,895 Apr-18-2022, 03:23 PM
Last Post: bowlofred
  How to read rainfall time series and insert missing data points MadsM 4 2,186 Jan-06-2022, 10:39 AM
Last Post: amdi40
  How to convert binary data into text? ZYSIA 3 2,643 Jul-16-2021, 04:18 PM
Last Post: deanhystad
  [Solved] Using readlines to read data file and sum columns Laplace12 4 3,562 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  Read/Write binary file deanhystad 3 3,202 Feb-01-2021, 10:29 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020