-
Notifications
You must be signed in to change notification settings - Fork 0
/
myFunctions.cpp
86 lines (78 loc) · 1.97 KB
/
myFunctions.cpp
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
82
83
84
85
86
#include "myFunctions.h"
std::string HexToString(std::string hex)
{
int len = hex.length();
std::string newString;
for(int i=0; i< len; i+=2)
{
std::string byte = hex.substr(i,2);
char chr = (char) (int)strtol(byte.c_str(), NULL, 16);
newString.push_back(chr);
}
return newString;
}
char * string2Char (std::string Input)
{
char *Output = new char[Input.length() + 1];
strcpy(Output, Input.c_str());
return (Output);
}
bool string2bool(std::string Input)
{
if (Input=="true")
return true;
else
return false;
}
std::string bool2string(bool input)
{
if (input)
return "true";
else
return "false";
}
int string2int(const std::string str, int *p_value)
{
// wrapping string2int because it may throw an exception
std::size_t* pos = 0;
int base = 10;
try {
*p_value = std::stoi(str, pos, base);
return 0;
}
catch (const std::invalid_argument& ia) {
//std::cerr << "Invalid argument: " << ia.what() << std::endl;
return -1;
}
catch (const std::out_of_range& oor) {
//std::cerr << "Out of Range error: " << oor.what() << std::endl;
return -2;
}
catch (const std::exception& e)
{
//std::cerr << "Undefined error: " << e.what() << std::endl;
return -3;
}
}
int string2float(const std::string str, float *p_value)
{
// wrapping string2int because it may throw an exception
std::size_t* pos = 0;
try {
*p_value = std::stof(str, pos);
return 0;
}
catch (const std::invalid_argument& ia) {
//std::cerr << "Invalid argument: " << ia.what() << std::endl;
return -1;
}
catch (const std::out_of_range& oor) {
//std::cerr << "Out of Range error: " << oor.what() << std::endl;
return -2;
}
catch (const std::exception& e)
{
//std::cerr << "Undefined error: " << e.what() << std::endl;
return -3;
}
}