linux编程--将十六进制数据输出到指定文件里

来源:互联网 发布:余华兄弟读后感知乎 编辑:程序博客网 时间:2024/06/11 09:32
//将十六进制数据输出到a.bin文件里#include <stdio.h>  #include <string.h>  #include <sys/types.h>  #include <sys/stat.h>  #include <fcntl.h>  #include <errno.h>  int main(int argc, char **argv)  {      int fd;      if(argc==1)    {    printf("outbin use format:\n  outbin <key>\n");    return 0;    }    remove("./a.bin");    fd = open("./a.bin", O_RDWR | O_CREAT,S_IRWXU);      if (fd == -1)      {          printf("open file open_file_test failed!\n%s\n", strerror(errno));          return -1;      }    char *buf=argv[1];  //存放十六进制内容    unsigned char raw;    if(buf=='\0')    {    printf("no data\n");    return 0;    }    char i=0;    do    {      char raw[]="ff";      strncpy(raw,buf+i,2);      unsigned char data=strtoul(raw,NULL,16);      //printf("data=%d\n",atoi("11"));      write(fd,&data,1);      i+=2;    }while(buf[i]);    close(fd);    printf("Out file:a.bin OK\n");    return 0;}  
0 0