進数変換

romophic-library

用途

n進数からm進数に変換

使い方

1
string res = convNotation(str,n,m);

実装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
string convNotation(const string &s, int from, int to) {
  int n = stoll(s, nullptr, from);
  if (n == 0)
    return "0";
  string result;
  while (n) {
    result += n % to < 10 ? '0' + n % to : 'A' + n % to - 10;
    n /= to;
  }
  if (s[0] == '-')
    result += '-';
  reverse(result.begin(), result.end());
  return result;
}
Licensed under CC BY-NC-ND 4.0
All rights reserved.
Built with Hugo
Theme Stack is designed by Jimmy