forked from SlyrithDevelopment/Unity-ImGUI-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKittyUtils.cpp
More file actions
81 lines (65 loc) · 2.64 KB
/
Copy pathKittyUtils.cpp
File metadata and controls
81 lines (65 loc) · 2.64 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "KittyUtils.h"
namespace KittyUtils {
void trim_string(std::string &str)
{
// https://www.techiedelight.com/remove-whitespaces-string-cpp/
str.erase(std::remove_if(str.begin(), str.end(), [](char c)
{ return (c == ' ' || c == '\n' || c == '\r' ||
c == '\t' || c == '\v' || c == '\f'); }),
str.end());
}
bool validateHexString(std::string &hex)
{
if (hex.empty()) return false;
if (hex.compare(0, 2, "0x") == 0)
hex.erase(0, 2);
trim_string(hex); // first remove spaces
if (hex.length() < 2 || hex.length() % 2 != 0) return false;
for (size_t i = 0; i < hex.length(); i++) {
if (!std::isxdigit((unsigned char) hex[i]))
return false;
}
return true;
}
// https://tweex.net/post/c-anything-tofrom-a-hex-string/
// ------------------------------------------------------------------
/*!
Convert a block of data to a hex string
*/
void toHex(
void *const data, //!< Data to convert
const size_t dataLength, //!< Length of the data to convert
std::string &dest //!< Destination string
) {
unsigned char *byteData = reinterpret_cast<unsigned char *>(data);
std::stringstream hexStringStream;
hexStringStream << std::hex << std::setfill('0');
for (size_t index = 0; index < dataLength; ++index)
hexStringStream << std::setw(2) << static_cast<int>(byteData[index]);
dest = hexStringStream.str();
}
// ------------------------------------------------------------------
/*!
Convert a hex string to a block of data
*/
void fromHex(
const std::string &in, //!< Input hex string
void *const data //!< Data store
) {
size_t length = in.length();
unsigned char *byteData = reinterpret_cast<unsigned char *>(data);
std::stringstream hexStringStream;
hexStringStream >> std::hex;
for (size_t strIndex = 0, dataIndex = 0; strIndex < length; ++dataIndex) {
// Read out and convert the string two characters at a time
const char tmpStr[3] = {in[strIndex++], in[strIndex++], 0};
// Reset and fill the string stream
hexStringStream.clear();
hexStringStream.str(tmpStr);
// Do the conversion
int tmpValue = 0;
hexStringStream >> tmpValue;
byteData[dataIndex] = static_cast<unsigned char>(tmpValue);
}
}
}