Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #define NMAX 101
- char *hex2text(const char *const hex) {
- const int n = strlen(hex) - 1;
- assert(n % 2 == 0);
- char *text = malloc(sizeof(char) * (n / 2 + 1));
- if (text == NULL) {
- return NULL;
- }
- text[0] = 0;
- for (int i = 0; i < n; i += 2) {
- char aux[3];
- strncpy(aux, hex + i, 2);
- text[i / 2] = strtol(aux, NULL, 16);
- }
- text[n / 2 + 1] = 0;
- return text;
- }
- int main() {
- char hex[NMAX], *text;
- if (fgets(hex, NMAX, stdin) != NULL) {
- text = hex2text(hex);
- if (text != NULL) {
- puts(text);
- return 0;
- }
- }
- puts("Error!");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement