进制转换
string ten_to_other(int y, int sum) {
string res = "";
while (sum != 0) {
char c = sum % y;
if (0 <= c && c <= 9) {
c = c + ‘0‘;
} else { // A-Z 对应的数值
c = c + ‘A‘ - 10;
}
sum = sum / y;
res = c + res; // 类似栈
}
return res;
}
string x_to_y(int x, int y, string s) {
string res = "";
int sum = 0;
int len = s.length();
for (int i = 0; i < len; i++) {
if (s[0] == ‘-‘) {
continue;
}
if (‘0‘ <= s[i] && s[i] <= ‘9‘) {
sum = sum * x + s[i] - ‘0‘;
} else if (‘A‘ <= s[i] && s[i] <= ‘Z‘) {
sum = sum * x + s[i] - ‘A‘;
}
}
while (sum != 0) {
char c = sum % y;
if (0 <= c && c <= 9) {
c = c + ‘0‘;
} else { // A-Z 对应的数值
c = c + ‘A‘ - 10;
}
sum = sum / y;
res = c + res; // 类似栈
}
return res;
}
Date: 2015-01-23 01:29:49
Author: sunx
Created: 2015-01-23 Fri 01:30
Emacs 24.4.2 (Org mode 8.2.10)
Validate