标签:div warning += main include pow nbsp war for
给出一个16进制,转换成10进制数
如:“F4” --> 244
16进制转换成10进制:A-F对应10-15
F4 = 15*16^1 + 4*16^0 = 244
主要是将字符转换成数字
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math.h> int hex_to_dec(char *a) { int len = strlen(a); int sum = 0; for (int i = 0;i < len;i++) { if(a[i] >= ‘A‘ && a[i] <= ‘F‘) { a[i] = int(a[i] - ‘A‘) + 10 + ‘0‘; } if(a[i] >= ‘a‘ && a[i] <= ‘f‘) { a[i] = int (a[i] - ‘a‘) + 10 + ‘0‘; } //pow(16.0,2)16的2次方 sum += (a[i] - ‘0‘) * (pow(16.0,len - 1 - i)); } return sum; } int main() { char a[10]; scanf("%s",&a); printf("%d\n",hex_to_dec(a)); }
标签:div warning += main include pow nbsp war for
原文地址:https://www.cnblogs.com/xiaokang01/p/12426218.html