题目:进制转换合集
#pragma once
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#include<string>
long stringToDecimal(const char* str, int base) {
long result = 0;
int multiplier = 1;
int len = strlen(str);
for (int i = len - 1; i >= 0; i--) {
char c = tolower(str[i]);
int digit = -1;
if (c >= '0' && c <= '9') {
digit = c - '0';
}
else if (base == 16 && (c >= 'a' && c <= 'f')) {
digit = c - 'a' + 10;
}
else if (c < '0' || (c > '9' && base != 16) || (base == 16 && c > 'f')) {
return 0;
}
if (digit >= base) {
return 0;
}
result += digit * multiplier;
multiplier *= base;
}
return result;
}
void convertToBaseR(int num, int r) {
int index = 0;
char result[65];
if (num == 0) {
printf("0\n");
return;
}
while (num > 0) {
int remainder = num % r;
if (remainder < 10) {
result[index++] = '0' + remainder;
}
else {
result[index++] = 'A' + (remainder - 10);
}
num /= r;
}
result[index] = '\0';
for (int i = index - 1; index >= 0; index--) {
printf("%c", result[i]);
}
printf("\n");
}