Rizin
unix-like reverse engineering framework and cli tools
hex2bin.c
Go to the documentation of this file.
1 //
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
10 //
12 
13 #include "sysdefs.h"
14 #include <stdio.h>
15 #include <ctype.h>
16 
17 
18 static int
19 getbin(int x)
20 {
21  if (x >= '0' && x <= '9')
22  return x - '0';
23 
24  if (x >= 'A' && x <= 'F')
25  return x - 'A' + 10;
26 
27  return x - 'a' + 10;
28 }
29 
30 
31 int
32 main(void)
33 {
34  while (true) {
35  int byte = getchar();
36  if (byte == EOF)
37  return 0;
38  if (!isxdigit(byte))
39  continue;
40 
41  const int digit = getchar();
42  if (digit == EOF || !isxdigit(digit)) {
43  fprintf(stderr, "Invalid input\n");
44  return 1;
45  }
46 
47  byte = (getbin(byte) << 4) | getbin(digit);
48  if (putchar(byte) == EOF) {
49  perror(NULL);
50  return 1;
51  }
52  }
53 }
#define NULL
Definition: cris-opc.c:27
static int getbin(int x)
Definition: hex2bin.c:19
int main(void)
Definition: hex2bin.c:32
int x
Definition: mipsasm.c:20
#define isxdigit(c)
Definition: safe-ctype.h:145
Common includes, definitions, system-specific things etc.