I need to convert hex encoded string like this:
char hstr[9] = "61626364"; // characters abcd\0
Into
"abcd" // characters as hex: 0x61 0x62 0x63 0x64
// hex "digits" a-f are always lowercase
At this moment I wrote this function:
#include <stdlib.h>
void htostr(char* hexstr, char* str) {
int len = strlen(hexstr);
for (int i = 0; i < len/2; i++) // edit: fixed bounds
{
char input[3] = { hexstr[2 * i], hexstr[2 * i + 1], 0 };
*(str + i) = (char)strtol(input, NULL, 16);
}
}
I'm using strtol
function to do the job.
I feel I'm wasting 3 bytes of memory for input
array and some processor time for copying two bytes and terminating with 0, because strtol
function has no parameter like "length".
The code is supposed to run on a pretty busy microcontroller, the strings are quite long (it would be a good idea to free up the memory used by hexstr
as soon as possible).
The question is: is there more efficient way to do this without writing my own converter from scratch?
By "from scratch" I mean low level conversion without using functions standard library.