-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_string.h
More file actions
63 lines (53 loc) · 1.29 KB
/
Copy pathconvert_string.h
File metadata and controls
63 lines (53 loc) · 1.29 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
#ifndef CPP_ALGORITHM_CONVERT_STRING_H
#define CPP_ALGORITHM_CONVERT_STRING_H
#include <string>
namespace ConvertString
{
/**
* \brief Convert integer to string.
* \param number input number
* \return number string
*/
std::string IntToString(int number);
/**
* \brief Convert string to integer.
* \param str input string
* \return number
*/
int StringToInt(const std::string& str);
// TODO: Implement ConvertBase
std::string ConvertBase(const std::string& str, int b1, int b2);
}
// ----------------------------------------------------------------------------
inline std::string ConvertString::IntToString(int number)
{
std::string str;
bool is_negative = false;
if (number < 0)
{
is_negative = true;
number = -number;
}
do
{
str.push_back('0' + number % 10);
number /= 10;
} while (number > 0);
if (is_negative)
{
str.push_back('-');
}
std::reverse(str.begin(), str.end());
return str;
}
// ----------------------------------------------------------------------------
inline int ConvertString::StringToInt(const std::string& str)
{
int number = 0;
for (char i : str)
{
number = (number * 10) + (i - '0');
}
return number;
}
#endif