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

C语言读取二进制文件时输出结果为16777216而非预期值1的原因是什么?

Why am I getting 16777216 instead of 1 when reading a binary file in C?

Let's break this down—you're not skipping any 0 bytes at all. The issue here is endianness (byte order), a super common pitfall when working with binary data across systems.

Looking at your xxd -b output, the first 4 bytes of your file are:

00000000 00000000 00000000 00000001

In hex terms, that's four bytes: 0x00, 0x00, 0x00, 0x01. This is a big-endian representation of the number 1—big-endian stores the most significant byte (the "big" part of the number) first at the lowest memory address.

But your computer uses little-endian byte order. When fread loads those four bytes into your uint32_t variable b, it places the first file byte (0x00) at the lowest memory address of b, and the last file byte (0x01) at the highest memory address. For a little-endian system, this means the byte at the highest address is the most significant byte. So your variable ends up holding:

0x01 0x00 0x00 0x00

Convert that to decimal, and you get 16777216 (which is 0x01000000 in hex)—exactly what your program prints.

How to fix this

You need to convert the big-endian data from the file to match your system's byte order. Here are a few reliable ways:

  1. Use standard byte-order conversion functions
    Most systems provide functions to convert between network byte order (which is big-endian) and host byte order (your system's native order). For 32-bit integers, use ntohl() (network-to-host long):

    #include <stdio.h>
    #include <stdint.h>
    #include <arpa/inet.h>  // For ntohl()
    
    int main(int argc, char const *argv[]) { 
        FILE * input_file = fopen("binary.bin", "rb"); 
        uint32_t b = 0 ; 
        fread(&b, sizeof(uint32_t), 1, input_file); 
        b = ntohl(b);  // Convert big-endian to host byte order
        printf("----- Here are the data stored in the file -----"); 
        printf("\nFirst uint_32 : %u\n", b);  // Use %u for unsigned integers!
        printf("------------------------------------------------\n"); 
        fclose(input_file);  // Don't forget to close the file!
        return 0; 
    }
    

    Note: I also changed %d to %u since you're printing an unsigned integer—using %d can cause unexpected behavior for values larger than INT_MAX.

  2. Manually reverse the bytes
    If you want to avoid platform-specific headers, you can manually shift and combine the bytes:

    uint32_t convert_big_to_host(uint32_t val) {
        return ((val >> 24) & 0xFF) |
               ((val >> 8) & 0xFF00) |
               ((val << 8) & 0xFF0000) |
               ((val << 24) & 0xFF000000);
    }
    
    // Then in your main function:
    b = convert_big_to_host(b);
    
  3. Read bytes individually and assemble the integer
    This gives you full control over the byte order, regardless of your system's native endianness:

    uint8_t bytes[4];
    fread(bytes, sizeof(uint8_t), 4, input_file);
    uint32_t b = (uint32_t)bytes[0] << 24 | 
                 (uint32_t)bytes[1] << 16 | 
                 (uint32_t)bytes[2] << 8 | 
                 bytes[3];
    

A quick reminder

Always document the byte order of your binary files! When writing binary data, it's standard practice to use big-endian (network byte order) to avoid cross-system issues. When reading, make sure you know the file's byte order and convert accordingly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 17:02:29