8000 Added utility functions for string/int conversion · m4k3r-org/esp32_https_server@65fb3a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 65fb3a1

Browse files
committed
Added utility functions for string/int conversion
1 parent 971d341 commit 65fb3a1

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

https/util.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* util.cpp
3+
*
4+
* Created on: Dec 17, 2017
5+
* Author: frank
6+
*/
7+
8+
9+
#include "util.hpp"
10+
11+
namespace httpsserver {
12+
13+
int parseInt(std::string s) {
14+
int i = 0; // value
15+
int m = 1; // multiplier
16+
17+
// Check sign
18+
size_t x = 0;
19+
if (s[0]=='-') {
20+
21+
x = 1;
22+
} else if (s[0]=='+') {
23+
x = 1;
24+
}
25+
26+
// Convert by base 10
27+
for(; x < s.size(); x++) {
28+
char c = s[x];
29+
if (c >= '0' && c<='9') {
30+
i = i*10 + (c-'0');
31+
} else {
32+
break;
33+
}
34+
}
35+
36+
// Combine both.
37+
return m*i;
38+
}
39+
40+
std::string intToString(int i) {
41+
// We need this much digits
42+
int digits = ceil(log10(i));
43+
char c[digits+1];
44+
c[digits] = '\0';
45+
46+
for(int x = digits-1; x >= 0; x--) {
47+
char v = (i%10);
48+
c[x] = '0' + v;
49+
i = (i-v)/10;
50+
}
51+
52+
return std::string(c);
53+
}
54+
55+
56+
}

https/util.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* util.h
3+
*
4+
* Created on: Dec 17, 2017
5+
* Author: frank
6+
*/
7+
8+
#ifndef HTTPS_UTIL_HPP_
9+
#define HTTPS_UTIL_HPP_
10+
11+
#include <Arduino.h>
12+
13+
#include <cmath>
14+
#include <string>
15+
16+
namespace httpsserver {
17+
18+
int parseInt(std::string s);
19+
20+
std::string intToString(int i);
21+
22+
}
23+
24+
#endif /* HTTPS_UTIL_HPP_ */

0 commit comments

Comments
 (0)
0