forked from SloCompTech/ByteConvert_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteConvert.cpp
More file actions
48 lines (47 loc) · 1.23 KB
/
ByteConvert.cpp
File metadata and controls
48 lines (47 loc) · 1.23 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
#include "ByteConvert.hpp"
namespace ByteConvert {
char valToHex(uint8_t val) {
if ((val & 0x0f) < 10)
return ('0' + val);
else
return ('a' + (val - 10));
}
uint8_t hexToVal(char c) {
if (c >= 'a')
return (15 - ('f' - c));
else
return (9 - ('9' - c));
}
std::string byteToHexString(uint8_t b) {
std::string buffer = "";
buffer += valToHex(b & 0x0f);
b >>= 4;
buffer = valToHex(b & 0x0f) + buffer;
return buffer;
}
uint8_t hexStringToByte(std::string block) {
if (block.length() != 2)
return 0x00;
return 16*hexToVal(block.at(0))+hexToVal(block.at(1));
}
std::string arrayToString(size_t size,uint8_t *src) {
std::string buffer = "";
for (size_t i = 0;i < (size);i++)
buffer += byteToHexString(src[i]);
return buffer;
}
uint8_t* stringToArray(size_t &size,std::string src) {
// Length of src must be odd !!
if (src.length() % 2 == 1)
src = "0" + src;
size = src.length()/2;
uint8_t *dst = new uint8_t[size]; // Allocate memory space
std::string buff = "";
for (size_t i = 0;i < (size);i++) {
buff = src.at(i*2);
buff += src.at(i*2+1);
dst[i] = hexStringToByte(buff);
}
return dst;
}
}