-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
44 lines (39 loc) · 884 Bytes
/
util.h
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
#include "BigInt.hpp"
#include <sstream>
#include <string>
namespace util
{
BigInt hexchar_to_int(char ch)
{
return (ch >= 'a') ? (ch - 'a' + 10) : (ch - '0');
}
auto pow(BigInt base, BigInt power)
{
BigInt value = 1;
for (unsigned int i = 0; i < power; i++)
{
value = value * base;
}
return value;
}
auto hexstream_to_bigint(std::string str)
{
BigInt val = 0;
auto len = str.length();
for (unsigned int i = 0; i < len; i++)
{
val = val + util::hexchar_to_int(str[i]) * util::pow(16, len - 1 - i);
}
return val;
}
auto int_to_hexstr(std::string val)
{
std::stringstream stream;
stream << std::hex << std::atoi(val.c_str());
return stream.str();
}
std::pair<BigInt, BigInt> divmod(BigInt dividend, BigInt divisor)
{
return std::make_pair(dividend / divisor, dividend % divisor);
}
} // namespace util