··································································································································
问题描述
从键盘输入一个不超过8位的正的十六进制数字符串,将它转换为正的十进制数后输出。
注:十六进制数中的10~15分别用大写的英文字母A、B、C、D、E、F表示。
样例输入
FFFF
样例输出
65535
··································································································································
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int main()
{
int i,j;
long long num = 0;
char str1[8],str2[33] = {0};
//char *str2;
// str2 = new char[33];
scanf("%s",str1);
for(j = 0 ; j < strlen(str1) ; j++)
{
switch(str1[j])//全部转换为二进制
{
case '0':strcat(str2,"0000");break;
case '1':strcat(str2,"0001");break;
case '2':strcat(str2,"0010");break;
case '3':strcat(str2,"0011");break;
case '4':strcat(str2,"0100");break;
case '5':strcat(str2,"0101");break;
case '6':strcat(str2,"0110");break;
case '7':strcat(str2,"0111");break;
case '8':strcat(str2,"1000");break;
case '9':strcat(str2,"1001");break;
case 'A':strcat(str2,"1010");break;
case 'B':strcat(str2,"1011");break;
case 'C':strcat(str2,"1100");break;
case 'D':strcat(str2,"1101");break;
case 'E':strcat(str2,"1110");break;
case 'F':strcat(str2,"1111");break;
default:break;
}
}//转换为二进制完毕
//cout<<str1<<endl;
//cout<<str2;
int len = strlen(str2);//二进制数长度;
i = 0;
for(j = len - 1 ; j >= 0 ; j --)//1101
{
if((str2[j]-'0') == 1)
num = num + pow(2,i++);
else
i++;
}
cout<<num;
printf("\n");
return 0;
}
额外有关于这道题的:
%u,%o,%X,%x,分别是用于输入和输出无符号的十进制、八进制、十六进制(大写小写)证书的。
·········································································································
问题描述
十六进制数是在程序设计时经常要使用到的一种整数的表示方式。它有0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F共16个符号,分别表示十进制数的0至15。十六进制的计数方法是满16进1,所以十进制数16在十六进制中是10,而十进制的17在十六进制中是11,以此类推,十进制的30在十六进制中是1E。
给出一个非负整数,将它表示成十六进制的形式。
输入格式
输入包含一个非负整数a,表示要转换的数。0<=a<=2147483647
输出格式
输出这个整数的16进制表示
样例输入
30
样例输出
1E
··········································································································
#include<stdio.h>
int main()
{
int num;
scanf("%d",&num);//输入十进制
if(num >= 0 && num <= 2147483647)
printf("%X",num);
return 0;
}
//最简单的一种······
不过最简单的一钟也是最实用的啦,用上面的switch也可以直接十转十六,繁琐一点,方法一致我就不赘述了!!