andrewfaseuk@hotmail.com wrote:[color=blue]
> 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';[/color]
You've overflowed your buffer here. It only has elements with indexes 0
and 1 and you write to element 2 which is out of bounds.
[color=blue]
> 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) {[/color]
Here you call strlen() on each iteration. A better idea would be to
call it once before the loop.
[color=blue]
> byte[0] = argv[2][i];
> byte[1] = argv[2][i+1];
> l = strtol(byte,NULL,16);[/color]
Here strtol may go wild, since byte buffer has only two elements and
its not zero terminated.
[color=blue]
> 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 ?[/color]
You also don't check for invalid input here.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
inline int is_hex(char c)
{
return (c >= '0' && c <= '9')
|| ((c | 0x20) >= 'a' && (c | 0x20) <= 'f')
;
}
inline unsigned char hex2bin(unsigned char h, unsigned char l)
{
h |= 0x20; // to lower
h -= 0x30;
h -= -(h > 9) & 0x27;
l |= 0x20;
l -= 0x30;
l -= -(l > 9) & 0x27;
return h << 4 | l;
}
int main(int ac, char** av)
{
if(ac < 2)
return EXIT_FAILURE; // wrong number of arguments
size_t nibbles = strlen(av[1]);
if(nibbles % 2) // odd number of nibbles
return EXIT_FAILURE;
nibbles /= 2;
unsigned char* buf = (unsigned char*)malloc(nibbles);
if(!buf) // out of memory
return EXIT_FAILURE;
for(size_t n = 0; n != nibbles; ++n)
{
if(!is_hex(av[1][2 * n]) || !is_hex(av[1][2 * n + 1]))
return EXIT_FAILURE; // bad input hex string
buf[n] = hex2bin(av[1][2 * n], av[1][2 * n + 1]);
}
for(size_t n = 0; n != nibbles; ++n)
printf("%02hhx ", buf[n]);
printf("\n");
}