---
# System prepended metadata

title: Base conversion
tags: [codebook]

---

---
tags: codebook
---

{%hackmd theme-dark %}

# Base conversion

```cpp=
long long pow(long long a, int b) {
	int ret = 1;
	for (; b; b >>= 1, a *= a)
		if (b & 1) ret *= a;
	return ret;
}
const string& base_cvrt(string& str, int n, int m) {
	int val = 0;
	for (size_t i = 0; i != str.size(); i++)
		val += (str[str.size() - i - 1] + (isalpha(str[str.size() - i - 1]) ? -'A' + 10 : -'0')) * pow(n, i);
	string().swap(str);
	while (val) str += val % m + (val % m >= 10 ? 'A' - 10 : '0'), val /= m;
	reverse(str.begin(), str.end());
	return str;
}
```