Rizin
unix-like reverse engineering framework and cli tools
hex2bin.c File Reference

Converts hexadecimal input strings to binary. More...

#include "sysdefs.h"
#include <stdio.h>
#include <ctype.h>

Go to the source code of this file.

Functions

static int getbin (int x)
 
int main (void)
 

Detailed Description

Converts hexadecimal input strings to binary.

Definition in file hex2bin.c.

Function Documentation

◆ getbin()

static int getbin ( int  x)
static

Definition at line 19 of file hex2bin.c.

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 }
int x
Definition: mipsasm.c:20

References x.

Referenced by main().

◆ main()

int main ( void  )

Definition at line 32 of file hex2bin.c.

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
#define isxdigit(c)
Definition: safe-ctype.h:145

References getbin(), isxdigit, and NULL.