文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>linux下以二进制格式显示文件内容源码

linux下以二进制格式显示文件内容源码

时间:2010-11-05  来源:yqliu29

#include <stdio.h>

int main(int argc, char **argv)
{
    size_t length;
    unsigned char buffer[0x100000];
    FILE *fp;
    int i, j;

    if (argc != 2)
    {
        printf("Usage: binary filename\n");
        return 0;
    }

    fp = fopen(argv[1], "r");
    if (fp == NULL)
    {
        printf("No file named %s\n", argv[1]);
        return 0;
    }

    length = fread(&buffer[0], 1, 0x100000, fp);

    fclose(fp);

    for (i = 0; i < length; i++)
    {
         if ((i % 16) == 0)
        {
            printf("\n%08x: ", i);
        }

        printf("%02x ", buffer[i]);

        if ((i % 16) == 15)
        {
            printf("|");
            for (j = i - 15; j <= i; j++)
            {
                if ((buffer[j] >= 33) && (buffer[j] <= 126) && (buffer[j] != 0))
                {
                    printf("%c", buffer[j]);
                }
                else
                {
                    printf(".");
                }
            }
        }
    }
    
    for (j = i % 16; j < 16; j++)
    {
        printf(" ");
    }

    printf("|");

    for (j = (i / 16) * 16; j < i; j++)
    {
        if ((buffer[j] >= 33) && (buffer[j] <= 126) && (buffer[j] != 0))
        {
            printf("%c", buffer[j]);
        }
        else
        {
            printf(".");
        }
    }
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载