1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// This file is part of helpers4.
// Copyright (C) 2025 baxyz
// SPDX-License-Identifier: LGPL-3.0-or-later
use decode_into;
use DecodeError;
/// Decodes a hexadecimal string (either case) into bytes.
///
/// The string must be made of digit pairs only: surrounding whitespace, a `0x` prefix or
/// separators are errors, so trim or strip them first.
///
/// # Arguments
///
/// - `hex` - The hexadecimal string to decode.
///
/// # Errors
///
/// [`DecodeError::OddLength`] for an odd number of characters, [`DecodeError::InvalidChar`] for
/// a character that is not a hex digit.
///
/// # Examples
///
/// ```
/// use helpers4::hex::decode;
///
/// assert_eq!(decode("DeadBeef")?, vec![0xde, 0xad, 0xbe, 0xef]);
/// assert!(decode("abc").is_err());
/// # Ok::<(), helpers4::hex::DecodeError>(())
/// ```