-
Notifications
You must be signed in to change notification settings - Fork 2
/
xor.c
51 lines (40 loc) · 1.17 KB
/
xor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* Get two hexadecimal strings and return the xor.
*/
#include <string.h> // strlen
#include <stdio.h> // printf
#include <stdlib.h> // exit
int a2v(char c) {
if ((c >= '0') && (c <= '9')) return c - '0';
if ((c >= 'a') && (c <= 'f')) return c - 'a' + 10;
if ((c >= 'A') && (c <= 'F')) return c - 'A' + 10;
else {
fflush(stdout);
fprintf(stderr, "\nBad hexadecimal character '%c'.\n", c);
exit(2);
}
}
char v2a(int c) {
const char hex[] = "0123456789abcdef";
return hex[c];
}
int main(int argc, const char **argv) {
int i;
if (argc != 3) {
printf("Get two hexadecimal strings and return the xor.\n");
printf("Usage %s <hextringA> <hextringB>\n", argv[0]);
printf("Ex.: %s a1f6258c877d5fcd8964484538bfc92c a1f6258c877d5fcd8964484538bfc92c\n", argv[0]);
exit(1);
}
if (strlen(argv[2]) != strlen(argv[1])) {
fprintf(stderr, "Both strings must be the same length.\n");
exit(1);
}
for (i = 0; i < strlen(argv[1]); i++) {
int a = a2v(argv[1][i]);
int b = a2v(argv[2][i]);
int x = a^b;
putchar(v2a(x));
}
puts("");
}