I would like to optimize my code and avoid errors, I have this function that does the "work" but I think I can improve and avoid memory problems.
void function(char* message)
{
char * pointer;
unsigned char buffer[2048] = {0};
int buffer_len = 0;
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, message);
buffer_len = strlen(buffer);
memset(buffer, 0, sizeof(&buffer));
for(int i = 0, pointer = message; i < (buffer_len / 2); i++, pointer += 2)
{
sscanf(pointer, "%02hhX", &buffer[i]);
}
}
The idea of the function is to receive a string of this style "0123456789" and pass it to 0x01, 0x23, 0x45, ... in an unsigned char array. Any tip, good practice or improvement would be very useful.
The ussage is something like this:
function("0123456789");
In the function buffer ends like:
buffer[0] = 0x01
buffer[1] = 0x23
...
sizeof(&buffer)???