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

如何实现二进制文件转文本文件?ZedBoard向PC传数据存文件问询

Convert Binary File from ZedBoard to Text File

Hey there! Let's walk through how to turn that binary file you're getting from your ZedBoard into a human-readable text file. I'll cover both code-based approaches (since you're already working with C for your ZedBoard project) and quick command-line tricks that save you from writing extra code.

Option 1: Integrate Conversion into Your Existing C Code

If you want to either convert an existing binary file or even write text directly as you receive data, here's how to do it in C:

1.1 Standalone Binary-to-Text Converter

This program reads your saved binary file and outputs a formatted text file, matching the 4-column structure of your test_value array:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <input_binary> <output_text>\n", argv[0]);
        return 1;
    }

    FILE *bin_file = fopen(argv[1], "rb");
    if (!bin_file) {
        perror("Couldn't open binary file");
        return 1;
    }

    FILE *txt_file = fopen(argv[2], "w");
    if (!txt_file) {
        perror("Couldn't open text file");
        fclose(bin_file);
        return 1;
    }

    unsigned char byte;
    int byte_count = 0;
    // Read each byte, print as 2-digit hex, wrap every 4 bytes
    while (fread(&byte, sizeof(unsigned char), 1, bin_file) == 1) {
        fprintf(txt_file, "%02X ", byte);
        byte_count++;
        if (byte_count % 4 == 0) {
            fprintf(txt_file, "\n");
        }
    }

    // Add a final newline if the last line isn't complete
    if (byte_count % 4 != 0) {
        fprintf(txt_file, "\n");
    }

    fclose(bin_file);
    fclose(txt_file);
    printf("Conversion finished! Check %s\n", argv[2]);
    return 0;
}

Compile it with gcc converter.c -o bin2txt, then run ./bin2txt your_data.bin output.txt. The output will have each row matching the 4-value structure of your original test_value array.

1.2 Write Text Directly When Receiving Data

Instead of saving to binary first, you can modify your process_echo_request function to write readable text as you get data. For example, if you want to log the my_buffer or test_value data:

// Add this at the top of your function (or open the file once outside)
FILE *log_file = fopen("zedboard_data.txt", "a"); // "a" appends to the file
if (!log_file) {
    perror("Failed to open log file");
    return;
}

// Write my_buffer data to text
for (int k = 0; k < 4; k++) {
    fprintf(log_file, "%02X ", my_buffer[k]);
}
fprintf(log_file, "\n");

// Or write the entire test_value array
for (int i = 0; i < ROW; i++) {
    for (int j = 0; j < COLUMN; j++) {
        fprintf(log_file, "%02X ", test_value[i][j]);
    }
    fprintf(log_file, "\n");
}

fclose(log_file);

This way, you skip the binary file step entirely and get a readable log right away.

Option 2: Quick Command-Line Tools (No Code Needed)

If you already have the binary file saved, use these Linux tools to convert it in seconds:

Use xxd (Hex Format)

  • Basic hex conversion:
    xxd your_binary_file.bin > output.txt
    
  • Match your 4-column array structure:
    xxd -g 4 -c 4 your_binary_file.bin > output.txt
    
    The -g 4 groups bytes into 4-byte chunks, -c 4 limits each line to 4 bytes, so the output mirrors your test_value layout.

Use od (Decimal or Hex)

  • Output as unsigned decimal values (one per byte):
    od -t u1 -w4 -v your_binary_file.bin > output.txt
    
    -t u1 = unsigned 1-byte decimal, -w4 = 4 bytes per line, -v = show all lines (no duplicates omitted).
  • Output as hex values:
    od -t x1 -w4 -v your_binary_file.bin > output.txt
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:25:47