Skip to content

Instantly share code, notes, and snippets.

Last active May 22, 2019
What would you like to do?
Best hex2bin function I came up with.
By Zibri
Valid strings examples:
"1 d0 600d c0FFeE"
Spaces are ignored.
Parsing stops at the first non hex character.
Mixed case accepted.
int hex2bin_by_zibri(char *source_str, char *dest_buffer)
char *line = source_str;
char *data = line;
int offset;
int read_byte;
int data_len = 0;
while (sscanf(data, " %02x%n", &read_byte, &offset) == 1) {
dest_buffer[data_len++] = read_byte;
data += offset;
return data_len;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment