diff --git a/Linux Bootstrap/hex.c b/Linux Bootstrap/hex.c index ce4461d..81097df 100644 --- a/Linux Bootstrap/hex.c +++ b/Linux Bootstrap/hex.c @@ -21,7 +21,7 @@ void purge_line_comments() { - char c; + int c; do { c = getchar(); @@ -33,14 +33,19 @@ void purge_line_comments() } while ( '\n' != c ); } -int hex(char c) +int hex(int c) { - switch(c) + if (c >= '0' && c <= '9') { - case '0' ... '9': return (c - 48); - case 'a' ... 'f': return (c - 87); - case 'A' ... 'F': return (c - 55); - default: break; + return (c - 48); + } + else if (c >= 'a' && c <= 'z') + { + return (c - 87); + } + else if (c >= 'A' && c <= 'Z') + { + return (c - 55); } printf("You managed to call a hex function without a hex value!!!\n"); @@ -49,35 +54,32 @@ int hex(char c) int main(int argc, char *argv[]) { - char c; - int sum; + int c; + int sum = 0; bool toggle; toggle = false; do { c = getchar(); - switch(c) + if (c == '#') { - case '0' ... '9': - case 'a' ... 'f': - case 'A' ... 'F': + purge_line_comments(); + } + else if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) + { + if(!toggle) { - if(!toggle) - { - sum = hex(c); - toggle = true; - } - else - { - sum = (sum * 16) + hex(c); - toggle = false; - putc(sum, stdout); - } - break; + sum = hex(c); + toggle = true; } - case '#': purge_line_comments(); - default: break; + else + { + sum = (sum * 16) + hex(c); + toggle = false; + putc(sum, stdout); + } + break; } }while(c != EOF);