如何将.bin文件,hex方式查看里面数据。以自己需要的任何长度来分割

liunux环境编译命令:

gcc test.cpp -o test

test.cpp 文件:
instring 为需要被分割的文件:
outstring 为分割后的文件:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h> // Contains POSIX terminal control definitions


#define BUF_LENGTH  0x1869ff

int main(int argc, char * argv[])
{

   char *instring="./POTKCheck_00199D_0435_20000001_100000_20240321.chk";
   char *outstring="./POTKCheck_00199D_0435_20000001_100000_20240322.txt";
   FILE *in, *out;
   __uint8_t ch;
   printf("read file %s\n",instring);
   __uint8_t buf[BUF_LENGTH];
   int in_fd=open(instring,O_RDONLY);
   int out_fd=open(outstring,O_RDWR|O_CREAT,0666);
    if(in_fd==-1)
    {
            printf("read file fail\n");
            close(in_fd);
            return -1;
    }
    else{
            printf("read file success\n");
            read(in_fd,buf,BUF_LENGTH);
            close(in_fd);
    }

    in = fopen(instring, "r");
    out = fopen(outstring, "w");
    int i=0;
    while (fscanf(in, "%c", &ch) != EOF) {
        if((i%16==0)&&(i>0)) // 这个16代表每行16个字节
        {
            fprintf(out,"\r\n");
        }
        i++;
        fprintf(out, "%02X ", ch);
        if(i>BUF_LENGTH)
          break;
    }


        fclose(in);   
        fclose(out);

    return 0;

}

分割后如下图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baidu_37552881

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值