-
-
Save cwage/5842994 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
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