/* Test hash function to convert "abcdefABCDEF" to hexadecimal. */ #include int main(void) { int correct, expected, actual, i, d; correct = 0; /* Input is already adjusted to match decimal digits, so the search range comes with a -48 adjustment. */ for(i = 0; i < 256 - 48; i++) { expected = (i >= 'a' - 48 && i <= 'f' - 48) || (i >= 'A' - 48 && i <= 'F' - 48) ? (i & 15) + 9 : -1; d = i; #if 0 actual = (d-- & ~39) - 16 ? -1 : (d &= 15) < 6 ? d + 10 : -1; #else actual = (d & ~39) - 16 || ~-d % 16 > 5 ? -1 : d % 16 + 9; #endif if( (expected < 0 && actual < 0) || (expected == actual) ) correct++; } if( correct == 256 - 48 ) puts("ok"); return 0; }