A
andrewfaseuk
Hi,
Before I start I have a very basic knowledge of C++ so please be kind
!
I need to convert a input string i.e. from argv[] in the form
'FFEEDD....' containing hex values into a real hex coded string.
For example the input string :
AABBCCDDEEFF
need to be converted to the same as the below definition:
unsigned char data[6] = "\xAA\xBB\xCC\xDD\xEE\xFF";
I've managed to do this but its probably not the best solution, my
code is:
char byte[2];
byte[2] = '\0';
unsigned char *data2 = static_cast <unsigned char*>
(malloc((strlen(argv[2]))/2+1));
int i;
int j=0;
long l;
for (i=0; i < strlen(argv[2])-1;i=i+2) {
byte[0] = argv[2];
byte[1] = argv[2][i+1];
l = strtol(byte,NULL,16);
data2[j++] = (char)l;
}
data2[j] = '\0';
argv[2] contains the info I want to convert and data2 is the output
char array. Is there an easier way than this ? am I being very
unefficent ?
Answers appreciated
Before I start I have a very basic knowledge of C++ so please be kind
!
I need to convert a input string i.e. from argv[] in the form
'FFEEDD....' containing hex values into a real hex coded string.
For example the input string :
AABBCCDDEEFF
need to be converted to the same as the below definition:
unsigned char data[6] = "\xAA\xBB\xCC\xDD\xEE\xFF";
I've managed to do this but its probably not the best solution, my
code is:
char byte[2];
byte[2] = '\0';
unsigned char *data2 = static_cast <unsigned char*>
(malloc((strlen(argv[2]))/2+1));
int i;
int j=0;
long l;
for (i=0; i < strlen(argv[2])-1;i=i+2) {
byte[0] = argv[2];
byte[1] = argv[2][i+1];
l = strtol(byte,NULL,16);
data2[j++] = (char)l;
}
data2[j] = '\0';
argv[2] contains the info I want to convert and data2 is the output
char array. Is there an easier way than this ? am I being very
unefficent ?
Answers appreciated