#include<iostream> using namespace std; //十进制 dec //十六进制:hex //八进制:oct int main() { int a; while(cin>>hex>>a) //读入六进制 { cout<<a; cout<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll XToDec(string s,ll x) { //将给定的x进制字符串s转换成10进制 ll ans = 0; for(ll i = 0; i < s.length(); i++) { ans = ans*x + (isdigit(s[i]) ? s[i]-'0' : s[i]-'A'+10); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); string s; while(cin >> s) { s = s.substr(2,s.length()-2); cout << XToDec(s,16) << endl; } return 0; }
#include<iostream> (720)#include<string.h> #include<math.h> using namespace std; int main(){ char s[1000]; while(gets(s)){ int m=0,sum=0; int len=strlen(s); for(int i=len-1;i>=0;i--){ if(s[i]>='0'&&s[i]<='9') sum=sum+(s[i]-'0')*pow(16,m); else if(s[i]>='A'&&s[i]<='F') sum=sum+(s[i]-'A'+10)*pow(16,m); else if(s[i]>='a'&& s[i]<='f') sum=sum+(s[i]-'a'+10)*pow(16,m); m++; } cout<<sum<<endl; } }
//同不知道考察什么,这个进制转化蛮简单的,就是注意一下switch不要把break忘记了 #include<iostream> #include<cstring> #include<algorithm> using namespace std; int main(){ char a[20],b[20]; while(cin>>a){ int len=strlen(a),total=0; for(int i=len-1;i>1;i--) b[len-i-1]=a[i]; for(int i=0;i<len-2;i++) switch(b[i]){ case '0': total+=0*pow(16,i);break; case '1': total+=1*pow(16,i);break; case '2': total+=2*pow(16,i);break; case '3': total+=3*pow(16,i);break; case '4': total+=4*pow(16,i);break; case '5': total+=5*pow(16,i);break; case '6': total+=6*pow(16,i);break; case '7': total+=7*pow(16,i);break; case '8': total+=8*pow(16,i);break; case '9': total+=9*pow(16,i);break; case 'A': total+=10*pow(16,i);break; case 'B': total+=11*pow(16,i);break; case 'C': total+=12*pow(16,i);break; case 'D': total+=13*pow(16,i);break; case 'E': total+=14*pow(16,i);break; case 'F': total+=15*pow(16,i);break; } cout<<total<<endl; } return 0; }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc= new Scanner(System.in); while (sc.hasNext()) { String str=sc.nextLine(); int len=str.length()-2; int num=0; int y=0; for(int i=str.length()-1;i>=2;i--) { if(str.charAt(i)=='A')num+=10*toten(y); else if(str.charAt(i)=='B')num+=11*toten(y); else if(str.charAt(i)=='C')num+=12*toten(y); else if(str.charAt(i)=='D')num+=13*toten(y); else if(str.charAt(i)=='E')num+=14*toten(y); else if(str.charAt(i)=='F')num+=15*toten(y); else num+=Integer.parseInt(String.valueOf(str.charAt(i)))*toten(y); y++; } System.out.println(num); } } public static int toten(int a) { int x=1; if (a==0) return x; for (int i = 0; i < a; i++) { x*=16; } return x; } }
#include <iostream>
#include <string>
using namespace std;
unsigned long HextoDec(string s)
{
unsigned long sum = 0, *result = new unsigned long[s.length()];
for (int i = 0; i < s.length(); i++)
{
if (s[i] >= 'A' && s[i] <= 'F') result[i] = ((unsigned long)(s[i]) - 55) << (4 * (s.length() - 1 - i)); //m乘以的2的n次方 = m << n
else if (s[i] >= 'a' && s[i] <= 'f') result[i] = ((unsigned long)(s[i]) - 87) << (4 * (s.length() - 1 - i));
else if (s[i] >= '0' && s[i] <= '9') result[i] = (unsigned long)(s[i] - 48) << (4 * (s.length() - 1 - i));
sum += result[i];
}
return sum;
}
int main()
{
string s;
while (cin >> s)
{
string hex;
for(int i = 2; i < s.length(); i++) hex += s[i];
cout << HextoDec(hex) << endl;
}
return 0;
}
#include <iostream>#include <string>#include <cmath>using namespace std;intmain(){string str1;while(cin >> str1){string str2=str1.substr(2,str1.size()-2);intsum=0;for(inti = str2.size()-1;i>=0;--i){if(str2[i] >= '0'&& str2[i] <= '9')str2[i] -= 48;if(str2[i] >= 'A'&& str2[i] <= 'F')str2[i] -=55;sum += str2[i]*pow(16,str2.size()-i-1);}cout << sum << endl;}return0;}
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std ;
int len, res;
char str[ 1000 ];
int main()
{
while ( cin >> str )
{
res = 0 ;
len = strlen ( str );
int j = 0 ;
int cal = 0 ;
for ( int i= len - 1 ;i> 1 ;i--)
{
if ( str [i]>= ' 0 ' && str [i]<= ' 9 ' )
cal = str [i]- ' 0 ' ;
else if ( str [i]>= 'a' && str [i]<= 'z' )
cal = str [i]- 'a' + 10 ;
else
cal = str [i]- 'A' + 10 ;
res += cal* pow ( 16 ,j);
j++;
}
cout << res << endl ;
}
return 0 ;
}
十六进制转换十进制与二进制转换十进制同理,不同之处在于二进制的位权是2的指数,而十六进制的位权则是16的指数。针对本题输入的十六进制数值字符串除却开头的"0x",剩余位为有效位。对有效位从右至左依次升幂,同时乘以当位的基数,对每一位执行相同的如上操作,再将结果求和即可得十六进制数值对应的十进制数值。
import java.util.HashMap; import java.util.Scanner; public class Main { private static HashMap<Character, Integer> map; static { map = new HashMap<Character, Integer>(); map.put('A', 10); map.put('B', 11); map.put('C', 12); map.put('D', 13); map.put('E', 14); map.put('F', 15); } public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String input = in.nextLine(); System.out.println(toDecimal(input)); } } private static int toDecimal(String input) { //将十六进制数值字符串转换为十进制数值 int res = 0; int length = input.length() - 1; for (int i = input.length() - 1; i >= 2; i--) { //当前位字符 char cur = input.charAt(i); if (Character.isDigit(cur)) { //如果当前位字符为数字,直接将字符转为相应的int型数值,执行运算 res += Math.pow(16, length - i) * Integer.parseInt(cur + ""); } else { //如果当前位字符为A~F中的字母,根据十六进制规定,将其转换为相应的int型数值,执行运算 res += Math.pow(16, length - i) * map.get(cur); } } return res; } }
#include <bits/stdc++.h> using namespace std; int main(){ int x; while(scanf("%x", &x) == 1){ printf("%d\n", x); } return 0; }
#include <bits/stdc++.h> using namespace std; void pt(int x){ if(x > 9) pt(x / 10); putchar('0' + x % 10); } int main(){ int x; while(scanf("%x", &x) == 1){ pt(x); puts(""); } return 0; }