【问题标题】:How to print binary file output as Base 2 (in bits)?如何将二进制文件输出打印为 Base 2(以位为单位)?
【发布时间】:2020-09-29 01:48:35
【问题描述】:

我有一个 bin 文件,其中包含以字节存储的二进制数据。

尝试在 python 中读取它们时,输出类似于 \xb5D\xbe"jSUk\xe75\x18}@\'%\x89oRqR\xfb\xe9\xe9\

如何将文件内容打印为 Base 2 二进制文件? 比如10000000 01000000 11000000

【问题讨论】:

    标签: python binary


    【解决方案1】:

    这是一次读取 8 个字节并按照您描述的方式格式化它们的示例。

    请注意,您可能已经拥有可以执行类似任务的系统实用程序,例如类 Unix 系统上的 od 程序。

    with open("your_binary_file", "rb") as f:
        while True:
            data = f.read(8)
            if not data:
                break        
            print(" ".join(f"{byte:08b}" for byte in data))
    

    【讨论】:

      猜你喜欢
      • 2019-07-02
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 2018-11-25
      • 2015-07-11
      • 2012-09-02
      • 2017-09-24
      • 2018-09-15
      相关资源
      最近更新 更多