-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHex.h
More file actions
65 lines (51 loc) · 1.6 KB
/
Hex.h
File metadata and controls
65 lines (51 loc) · 1.6 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @file Hex.h
* @brief Hex is a lib, which has functions converts string to a string with hex values.
*
* Copyright (c) 2020-forever Vlad Rogozin (vlad.rogozin@bhcc.edu)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#ifndef RED_HEX_H
#define RED_HEX_H
#include <string>
#define REDHEX_VERSION "1.1"
namespace Red {
/**
* @brief GetHexArray
*
* @param a String to get hex string from.
*
* @return String with hex result.
*/
inline std::string * GetHexArray(const std::string_view a) {
std::string *Result = new std::string;
for (unsigned long long int i = 0; i < a.length(); i++) {
char const hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b','c','d','e','f'};
const unsigned char ch = (const unsigned char) a[i];
Result->append(&hex[(ch & 0xF0) >> 4], 1);
Result->append(&hex[ch & 0xF], 1);
}
return Result;
}
/**
* @brief GetStrArray
*
* @param a Hex string to get string from.
*
* @return Normal string.
*/
inline std::string * GetStrArray(const std::string_view a) {
std::string *Result = new std::string;
for (unsigned long long int n = 0; n < a.length(); n += 2) {
std::string HexByte = "";
HexByte += a[n];
HexByte += a[n + 1];
int num = std::stoi(HexByte, nullptr, 16);
Result->push_back((char) num);
}
return Result;
}
}
#endif // RED_HEX_H