Skip to content

Instantly share code, notes, and snippets.

@cwage
Created June 22, 2013 22:50
Show Gist options
  • Save cwage/5842960 to your computer and use it in GitHub Desktop.
Save cwage/5842960 to your computer and use it in GitHub Desktop.
mystring binarytohex(mystring source)
{
int i;
char chunk[4];
char hexchar;
mystring result;
result.text[0] = '\0';
for(i = 0; i < source.length; i++)
{
if (i > 0 && i % 4 == 0)
{
binaryquadtohexchar(chunk, hexchar);
}
chunk[i % 4] = source.text[i];
if (i == source.length-1)
{
binaryquadtohexchar(chunk, hexchar);
}
}
}
char binaryquadtohexchar(char * source, char result)
{
int i;
char * hexchars[][2] = {
{"0000", '0'},
{"0001", '1'},
{"0010", '2'},
{"0011", '3'},
{"0100", '4'},
{"0101", '5'},
{"0110", '6'},
{"0111", '7'},
{"1000", '8'},
{"1001", '9'},
{"1010", 'a'},
{"1011", 'b'},
{"1100", 'c'},
{"1101", 'd'},
{"1110", 'e'},
{"1111", 'f'},
};
for (i = 0; i <= 15; i++)
{
if (strcmp(source, hexchars[i][0]) == 0)
{
result = hexchars[i][1];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment