初始化字符串

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
using namespace std;
char file[]="hexstr.h";
bool str2hex(string str,string name)
{
    FILE* fp=fopen(file,"a+");
    fprintf(fp,"#define Init_%s() unsigned char %s[%d]={0};\\\n",name.c_str(),name.c_str(),str.length()+1);
    fprintf(fp,"{\\\n");
    char buf[10240]={0};
    sprintf(buf,"%s",str.c_str());
    for(int i=str.length()/2-1;i>=0;--i)
    {
        fprintf(fp,"%s[%d]=0x%x;\\\n",name.c_str(),i,buf[i]);
    }
    for(int i=str.length()/2;i<str.length();++i)
    {
        fprintf(fp,"%s[%d]=0x%x;\\\n",name.c_str(),i,buf[i]);
    }
    fprintf(fp,"}\\\n");
    fclose(fp);

    return false;
}
int main()
{
    ::remove(file);
    str2hex("darren","user");

    return 0;
}

 

posted on 2019-08-24 15:18  弘道者  阅读(562)  评论(0编辑  收藏  举报