summaryrefslogtreecommitdiff
path: root/AK/Hex.h
blob: 203ca4d5c802c0539db4f73a5ec0f1b494d9bef3 (plain)
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
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/ByteBuffer.h>
#include <AK/Error.h>
#include <AK/StringView.h>

#ifdef KERNEL
#    include <Kernel/KString.h>
#else
#    include <AK/DeprecatedString.h>
#endif

namespace AK {

constexpr u8 decode_hex_digit(char digit)
{
    if (digit >= '0' && digit <= '9')
        return digit - '0';
    if (digit >= 'a' && digit <= 'f')
        return 10 + (digit - 'a');
    if (digit >= 'A' && digit <= 'F')
        return 10 + (digit - 'A');
    return 255;
}

ErrorOr<ByteBuffer> decode_hex(StringView);

#ifdef KERNEL
ErrorOr<NonnullOwnPtr<Kernel::KString>> encode_hex(ReadonlyBytes);
#else
DeprecatedString encode_hex(ReadonlyBytes);
#endif

}

#if USING_AK_GLOBALLY
using AK::decode_hex;
using AK::decode_hex_digit;
using AK::encode_hex;
#endif