The program in the box below contains the function const char * convert(const char * inb,int idx)
that creates a buffer containing the data formatted as you require.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#define HEADER1_TEXT "const char msg_"
#define HEADER2_LEN 6 //The number of digits in the costants name msg_nnnn we want
#define HEADER3_TEXT "[] = {"
#define HEADERLENGTH (strlen(HEADER1_TEXT)+HEADER2_LEN+strlen(HEADER3_TEXT))
#define FOOTER "\n};\n"
#define FOOTERLENGTH (strlen(FOOTER))
#define INDENT "\n "
#define INDENTLENGTH (strlen(INDENT))
#define ITEMxROW 8
#define HEXFIELDLEN 6 // "0xnn, "
#define ROWLEN (INDENTLENGTH + (ITEMxROW * HEXFIELDLEN))
const char * convert(const char * inb,int idx);
const char * convert(const char * inb,int idx)
{
char * outb = NULL;
unsigned int l,rl=0,j;
l=strlen(inb);
if (l) {
j=l/2*HEXFIELDLEN - 1; //The char used to convert all digit with the format: "0xnn, "
rl=(j+ROWLEN)/ROWLEN; //Row number
j=j + rl*(INDENTLENGTH+1); // +1 The CR
j=j + HEADERLENGTH + FOOTERLENGTH;
outb = malloc(j+11); //+ 11 because the dimension is not correctly computed!!! :(
memset(outb,0,j+1);
if (outb!=NULL) {
strcpy(outb, HEADER1_TEXT);
sprintf(outb+strlen(outb),"%0" STR(HEADER2_LEN) "d",idx);
strcat(outb,HEADER3_TEXT);
rl=0;
for(j=0;j<l;j+=2) {
if (j)
strcat(outb,", ");
if ( (rl + HEADER2_LEN) > ROWLEN) {
rl=0;
}
if (!rl) {
rl=INDENTLENGTH;
strcat(outb,INDENT);
}
strcat(outb,"0x");
strncat(outb,inb+j,2);
rl+=HEXFIELDLEN;
}
}
}
return outb;
}
int main(int argc, char *argv[])
{
int i,retval=0;
char * buff=NULL;
if (argc<2) {
printf("Usage: %s hex-string [hex-string2] [...] [hex-stringn]\n",basename(argv[0]));
return 1;
}
for(i=1; i<argc; i++) {
buff=convert(argv[i],i);
if (buff!=NULL) {
strcat(buff,FOOTER);
printf("%s\n",buff);
free(buff);
} else {
retval=1;
break;
}
}
return retval;
}
The program-main calls the function convert
sending it the prompt parameters. To decode your string you have to compile the code (for example as convert) and then you have to run it from the system prompt using the string[s] to convert as parameter[s].
sysprompt: convert FE2A8D0000CA372D4F461B1D9A1883A32F018823FFFF60D30000484200000D0A0F270300030006000000B0040307000356A3
the output will be:
const char msg_000001[] = {
0xFE, 0x2A, 0x8D, 0x00, 0x00, 0xCA, 0x37, 0x2D,
0x4F, 0x46, 0x1B, 0x1D, 0x9A, 0x18, 0x83, 0xA3,
0x2F, 0x01, 0x88, 0x23, 0xFF, 0xFF, 0x60, 0xD3,
0x00, 0x00, 0x48, 0x42, 0x00, 0x00, 0x0D, 0x0A,
0x0F, 0x27, 0x03, 0x00, 0x03, 0x00, 0x06, 0x00,
0x00, 0x00, 0xB0, 0x04, 0x03, 0x07, 0x00, 0x03,
0x56, 0xA3
};
You may convert more strings. For example:
sysprompt: convert FE55ACA220 56ABFC 4587ABFD
the output will be:
const char msg_000001[] = {
0xFE, 0x55, 0xAC, 0xA2, 0x20
};
const char msg_000002[] = {
0x56, 0xAB, 0xFC
};
const char msg_000003[] = {
0x45, 0x87, 0xAB, 0xFD
};
P.S.: In the convert functions there's a piece of code that computes the dimension of the buffer to be used (and allocated) for the conversion. This piece of code is not complitely correct, execuse me! I think you can solve this problem!