Last active
April 1, 2020 01:24
-
-
Save hxhb/9019a284ae9b6cfa1436414e04145d59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdbool.h> | |
#define MAX_NUM 512 | |
void remove_suffix(const char* filename,char* resault); | |
void append_string(char* base_string,const char* append_string); | |
void replace_char(char* base_string,char origin,char target); | |
void printUsage() | |
{ | |
printf("Usage:\n\tbin2hex.exe $READ_MODE $FILEs\n$READ_MODE Options:\n\trb\t(read binary)\n\tr\t(read text)\n"); | |
} | |
int main(int argc,char* argv[]) | |
{ | |
if(argc < 3) | |
{ | |
printf("Error:The arguments should great then 1.\n"); | |
printUsage(); | |
return -1; | |
} | |
char file_name_cache[MAX_NUM]; | |
memset(file_name_cache, 0, MAX_NUM); | |
bool is_rb_mode = (strcmp(argv[1],"rb") == 0); | |
bool is_r_mode = (strcmp(argv[1],"r") == 0); | |
if(!(is_rb_mode || is_r_mode)) | |
{ | |
printUsage(); | |
return -1; | |
} | |
for(int fileIndex=2;fileIndex<argc;++fileIndex) | |
{ | |
FILE* fp=fopen(argv[fileIndex], argv[1]); | |
if(fp==NULL) | |
{ | |
printf("Error:Open File \"%s\" faild.\n",argv[fileIndex]); | |
continue; | |
}else{ | |
memset(file_name_cache, 0, MAX_NUM); | |
strcpy(file_name_cache,argv[fileIndex]); | |
} | |
char local_file_name[MAX_NUM]; | |
replace_char(file_name_cache, '.', '_'); | |
strcpy(local_file_name,file_name_cache); | |
append_string(file_name_cache, ".h"); | |
FILE* savefilefd=fopen(file_name_cache,"wb"); | |
fprintf(savefilefd,"// origin file: %s\n",argv[fileIndex]); | |
remove_suffix(argv[fileIndex], file_name_cache); | |
fprintf(savefilefd, "static const unsigned char\n %s_data[]=\n {\n\t",local_file_name); | |
for(int fileItem=0,count=1;(fileItem=getc(fp))!=EOF;++count) | |
{ | |
fprintf(savefilefd,"0x%02x,",fileItem & 0xff); | |
if(count>1 && count%12==0) | |
{ | |
fprintf(savefilefd, "\n\t"); | |
} | |
} | |
if(is_r_mode) | |
{ | |
fprintf(savefilefd,"0x00"); | |
} | |
fprintf(savefilefd,"\n };\n"); | |
fclose(fp); | |
fclose(savefilefd); | |
} | |
return 0; | |
} | |
void remove_suffix(const char* filename,char* resault) | |
{ | |
if(!resault) | |
return; | |
memset(resault,0,MAX_NUM); | |
char *pos=strstr(filename, "."); | |
if(pos) | |
{ | |
int posIndex=pos-filename; | |
for(int i=0; i<posIndex && i<MAX_NUM;++i) | |
{ | |
resault[i]=filename[i]; | |
} | |
} | |
} | |
void append_string(char* base_string,const char* append_string) | |
{ | |
if(!append_string) | |
return; | |
int basestring_len=strlen(base_string); | |
int append_string_len=strlen(append_string); | |
if((basestring_len+append_string_len) < MAX_NUM) | |
{ | |
for(int index=0;index < append_string_len;++index) | |
base_string[basestring_len+index]=append_string[index]; | |
} | |
} | |
void replace_char(char* base_string,char origin,char target) | |
{ | |
if(!base_string) | |
return; | |
for(char *p=base_string;(p=strchr(p, origin));++p) | |
{ | |
*p=target; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment