You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Qt和C++向.bin文件写入二进制(非十六进制)内容?

Hey there! Let's break this down clearly, starting with fixing a key misunderstanding you might have:

Core Concept Clarification

First off: The .bin file you've already generated is already a pure binary file. The "1234 5678 0000 0016" you're seeing is just a hexadecimal human-readable representation of the underlying binary data.

To put it simply:

  • Every byte in your .bin file is made of 8 bits (a mix of 0s and 1s) that computers can natively understand.
  • Hexadecimal is just a condensed way for us humans to read those bits (1 hex character = 4 bits). So 12 in hex translates to 00010010 in binary—they're the same data, just displayed differently. Your initial thought that "hex vs binary formats are equivalent" is close, but more accurately: hex is a representation of binary data, not a separate format.
What You're Actually Asking (And If It's Possible)

It sounds like you want a file that contains only the characters 0 and 1 (each bit written as a separate text character). That's totally feasible—but important note: this file will be a text file, not a traditional binary .bin file (since each 0/1 character takes up 1 full byte of storage, making the file 8x larger than your original .bin).

How to Convert Your facts.bin to a Pure 0/1 Text File

Here are two straightforward ways to do this, using your existing facts.bin (which holds the hex data 12 34 56 78 00 00 00 16):

Method 1: Python Script

This is flexible and easy to tweak if you need adjustments:

# Read the binary content of your .bin file
with open('facts.bin', 'rb') as input_file:
    byte_content = input_file.read()

# Convert each byte to an 8-bit binary string, then join everything together
binary_text = ''.join(format(byte, '08b') for byte in byte_content)

# Write the pure 0/1 string to a text file
with open('binary_output.txt', 'w') as output_file:
    output_file.write(binary_text)

When you run this, binary_output.txt will contain:
0001001000110100010101100111100000000000000000000000000000010110

Method 2: Command Line Tools (xxd + awk)

If you prefer using terminal commands, this one-liner works on most Unix-like systems (Linux, macOS, WSL):

xxd -b facts.bin | awk '{for(i=2;i<=NF;i++) printf "%s", $i}' > binary_output.txt

This uses xxd to convert the binary file to a binary bitstring, then awk strips out extra formatting and writes the pure 0/1 text to your output file.

Quick Recap
  • Your original facts.bin is already a binary file—hex is just how we read it, not what's stored.
  • Generating a text file full of 0s and 1s is totally doable with the methods above.
  • Just remember the difference between binary data (the raw bits computers use) and binary text (characters representing those bits).

内容的提问来源于stack exchange,提问作者Juliette Marquis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:09:33