I think you need to roll your own implementation. Here's one implementation taking inspiration from @mediocrevegetable
#include <stdlib.h>
#include <string.h>
static inline int bitstr_to_int(const char *str, int len)
{
int h = 0;
for (int i = 0; i < len; i++) {
switch (str[i]) {
case '0': h *= 2; continue;
case '1': h *= 2; h++; continue;
default: return -1;
}
}
return h;
}
char * bitstr_to_hexstr(const char *str, int len)
{
static const char hex_digit[] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'
};
int i = 0; //traces the input chars in bitstr
int j = 0; //traces the output chars in hexstr
int h = 0;
int r = len % 4;
int q = len / 4;
char *hexstr = malloc(q + !!r + 1);
if (hexstr == NULL) return NULL;
if (r != 0) {
h = bitstr_to_int(str, r);
if (h == -1) goto err;
hexstr[j++] = hex_digit[h];
}
for (i = r; i < len; i += 4) {
h = bitstr_to_int(str+i, 4);
if (h == -1) goto err;
hexstr[j++] = hex_digit[h];
}
hexstr[j] = '\0';
return hexstr;
err:
free(hexstr);
return NULL;
}
int main(void)
{
const char *key = "0111010111010101010101010100101011010";
char *res = bitstr_to_hexstr(key, strlen(key));
printf("%s\n", res);
}