Skip to content

Instantly share code, notes, and snippets.

@Zibri
Last active May 22, 2019 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zibri/992b38baa520eb478ef7fa15fd950a7a to your computer and use it in GitHub Desktop.
Save Zibri/992b38baa520eb478ef7fa15fd950a7a to your computer and use it in GitHub Desktop.
/*
Best hex2bin function I came up with.
By Zibri
Valid strings examples:
"1 d0 600d c0FFeE"
"01d0600dc0ffee"
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