Untitled
unknown
plain_text
2 years ago
1.2 kB
7
Indexable
#include <stdio.h>
void encode(char c) {
printf("%02X ", c);
}
void decode(char* str) {
int c;
if (str[0] >= '0' && str[0] <= '9') c = str[0] - '0';
else if (str[0] >= 'A' && str[0] <= 'F') c = str[0] - 'A' + 10;
else if (str[0] >= 'a' && str[0] <= 'f') c = str[0] - 'a' + 10;
else {
printf("n/a ");
return;
}
c *= 16;
if (str[1] >= '0' && str[1] <= '9') c += str[1] - '0';
else if (str[1] >= 'A' && str[1] <= 'F') c += str[1] - 'A' + 10;
else if (str[1] >= 'a' && str[1] <= 'f') c += str[1] - 'a' + 10;
else {
printf("n/a ");
return;
}
printf("%c ", c);
}
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("n/a\n");
return 1;
}
int mode = argv[1][0] - '0';
if (mode != 0 && mode != 1) {
printf("n/a\n");
return 1;
}
char input[3] = {0};
if (mode == 1) { // Decode mode
while (scanf("%2s", input) == 1) {
decode(input);
if (getchar() == '\n') break;
}
} else { // Encode mode
char c;
while ((c = getchar()) != '\n' && c != EOF) {
encode(c);
}
}
printf("\n");
return 0;
}
Editor is loading...
Leave a Comment