Skip to content

Instantly share code, notes, and snippets.

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