VariantKey  5.4.1
Numerical Encoding for Human Genetic Variants
hex.h
Go to the documentation of this file.
1 // VariantKey
2 //
3 // hex.h
4 //
5 // @category Libraries
6 // @author Nicola Asuni <nicola.asuni@genomicsplc.com>
7 // @copyright 2017-2018 GENOMICS plc
8 // @license MIT (see LICENSE)
9 // @link https://github.com/genomicsplc/variantkey
10 //
11 // LICENSE
12 //
13 // Copyright (c) 2017-2018 GENOMICS plc
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining a copy
16 // of this software and associated documentation files (the "Software"), to deal
17 // in the Software without restriction, including without limitation the rights
18 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 // copies of the Software, and to permit persons to whom the Software is
20 // furnished to do so, subject to the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be included in
23 // all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 // THE SOFTWARE.
32 
40 #ifndef VARIANTKEY_HEX_H
41 #define VARIANTKEY_HEX_H
42 
43 #include <inttypes.h>
44 #include <stdio.h>
45 
56 static inline size_t hex_uint64_t(uint64_t n, char *str)
57 {
58  return sprintf(str, "%016" PRIx64, n);
59 }
60 
67 static inline uint64_t parse_hex_uint64_t(const char *s)
68 {
69  uint64_t v = 0;
70  uint8_t b;
71  size_t i;
72  for (i = 0; i < 16; i++)
73  {
74  b = s[i];
75  if (b >= 'a')
76  {
77  b -= ('a' - 10); // a-f
78  }
79  else
80  {
81  if (b >= 'A')
82  {
83  b -= ('A' - 10); // A-F
84  }
85  else
86  {
87  b -= '0'; // 0-9
88  }
89  }
90  v = ((v << 4) | b);
91  }
92  return v;
93 }
94 
95 #endif // VARIANTKEY_HEX_H
static uint64_t parse_hex_uint64_t(const char *s)
Parses a 16 chars hexadecimal string and returns the code.
Definition: hex.h:67
static size_t hex_uint64_t(uint64_t n, char *str)
Returns uint64_t hexadecimal string (16 characters).
Definition: hex.h:56