SELKIELogger  1.0.0
DWHexPairs.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Swansea University
3  *
4  * This file is part of the SELKIELogger suite of tools.
5  *
6  * SELKIELogger is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * SELKIELogger is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this SELKIELogger product.
18  * If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 
25 #include "DWTypes.h"
26 
37 bool test(const char *in, const uint8_t target);
38 
44 int main(void) {
45  const char *ts1 = "00";
46  const char *ts2 = "01";
47  const char *ts3 = "0a";
48  const char *ts4 = "0A";
49  const char *ts5 = "10";
50  const char *ts6 = "ff";
51  const char *ts7 = "FF";
52 
53  bool res = true;
54 
55  res &= test(ts1, 0);
56  res &= test(ts2, 1);
57  res &= test(ts3, 10);
58  res &= test(ts4, 10);
59  res &= test(ts5, 16);
60  res &= test(ts6, 255);
61  res &= test(ts7, 255);
62 
63  return (res ? 0 : 1);
64 }
65 
72 bool test(const char *in, const uint8_t target) {
73  uint8_t val = 0;
74  bool res = hexpair_to_uint(in, &val);
75  if (!res) {
76  // LCOV_EXCL_START
77  fprintf(stderr, "Conversion of '%c%c' failed\n", in[0], in[1]);
78  return false;
79  // LCOV_EXCL_STOP
80  }
81 
82  if (val == target) {
83  fprintf(stdout, "Conversion of '%c%c' successfully yielded %d\n", in[0], in[1],
84  target);
85  return true;
86  }
87  // LCOV_EXCL_START
88  fprintf(stdout, "Conversion of '%c%c' failed (returned %d, not %d)\n", in[0], in[1], val,
89  target);
90  return false;
91  // LCOV_EXCL_STOP
92 }
int main(void)
Definition: DWHexPairs.c:44
bool test(const char *in, const uint8_t target)
Definition: DWHexPairs.c:72
bool hexpair_to_uint(const char *in, uint8_t *out)
Convert a string of hexadecimal characters to corresponding value.
Definition: DWTypes.c:34