本关任务:编写一个能够实现将以字符串方式存放的十六机制数转换为十进制数的函数C++
时间: 2024-06-01 12:09:58 浏览: 21
#include <stdio.h>
#include <string.h>
int hexToDec(char hex[]) {
int len = strlen(hex);
int base = 1;
int dec = 0;
for (int i = len - 1; i >= 0; i--) {
if (hex[i] >= '0' && hex[i] <= '9') {
dec += (hex[i] - '0') * base;
} else if (hex[i] >= 'A' && hex[i] <= 'F') {
dec += (hex[i] - 'A' + 10) * base;
} else if (hex[i] >= 'a' && hex[i] <= 'f') {
dec += (hex[i] - 'a' + 10) * base;
}
base *= 16;
}
return dec;
}
int main() {
char hex[100];
printf("请输入十六进制数:");
scanf("%s", hex);
printf("转换后的十进制数为:%d\n", hexToDec(hex));
return 0;
}
相关问题
用C++编写一个能够实现将以字符串方式存放的十六机制数转换为十进制数的函数
```c
#include <stdio.h>
#include <string.h>
#include <math.h>
int hex_to_decimal(char *hex);
int main(void) {
char hex[10];
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
printf("Decimal equivalent is: %d\n", hex_to_decimal(hex));
return 0;
}
int hex_to_decimal(char *hex) {
int decimal = 0, length = strlen(hex), base = 1, i;
for (i = length - 1; i >= 0; i--) {
if (hex[i] >= '0' && hex[i] <= '9') {
decimal += (hex[i] - '0') * base;
base *= 16;
} else if (hex[i] >= 'a' && hex[i] <= 'f') {
decimal += (hex[i] - 'a' + 10) * base;
base *= 16;
} else if (hex[i] >= 'A' && hex[i] <= 'F') {
decimal += (hex[i] - 'A' + 10) * base;
base *= 16;
}
}
return decimal;
}
```
注释:
- `hex_to_decimal` 函数用于将传入的十六进制字符串转换为十进制数,返回转换后的值。
- `decimal` 用于存储转换后的十进制值,`length` 存储字符串长度,`base` 用于计算当前字符的权值,`i` 用于循环遍历字符串。
- 在循环中,先判断当前字符是否为数字,如果是则直接计算,否则判断是否为小写字母或大写字母,如果是则需要将其转换为对应的数字进行计算。
- 最后返回转换后的十进制值。
编写一个能够实现将以字符串方式存放的十六机制数转换为十进制数的函数C++
#include <stdio.h>
#include <string.h>
#include <math.h>
int hexToDec(char hex[]) {
int len = strlen(hex);
int base = 1;
int dec = 0;
int i;
for (i = len - 1; i >= 0; i--) {
if (hex[i] >= '0' && hex[i] <= '9') {
dec += (hex[i] - 48) * base;
base *= 16;
}
else if (hex[i] >= 'a' && hex[i] <= 'f') {
dec += (hex[i] - 87) * base;
base *= 16;
}
else if (hex[i] >= 'A' && hex[i] <= 'F') {
dec += (hex[i] - 55) * base;
base *= 16;
}
}
return dec;
}
int main() {
char hex[] = "3A5F";
int dec = hexToDec(hex);
printf("Hex: %s\n", hex);
printf("Dec: %d\n", dec);
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)