在一行上输入一个十六进制数
,代表待转换的十六进制数,格式见题干。保证
转化得到的十进制数
的范围为
。
在一行上输出一个整数,代表
对应的十进制数。
0xFA93
64147
回忆十六进制转化为十进制的方法:从右往左,将第
位乘以
,然后求和。
在这个样例中,
的第
位是
,第
位是
,第
位是
,第
位是
,因此
。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str = in.nextLine().substring(2);
int num = 0;
for(int i=0;i<str.length();i++){
char s = str.charAt(str.length()-1-i);
int bit = switchToNum(String.valueOf(s).toUpperCase());
num += bit * Math.pow(16,i);
}
System.out.print(num);
}
}
public static int switchToNum(String s){
switch(s){
case "A": return 10;
case "B": return 11;
case "C": return 12;
case "D": return 13;
case "E": return 14;
case "F": return 15;
default: return Integer.valueOf(s);
}
}
} import java.util.Scanner;
import java.util.Stack;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.next();
str = str.substring(2, str.length());
//System.out.println(str);
Stack stack = new Stack<Character>();
for (int i = 0; i < str.length(); i++) {
stack.add(str.charAt(i));
}
int result = 0;
int size = stack.size();
for (int i = 0; i < size; i++) {
Character c = (Character)stack.pop();
if (Character.isDigit(c)) {
Integer c1 = Integer.valueOf(c.toString());
result += c1 * Math.pow(16, i);
} else {
if (c == 'A') {
result += 10 * Math.pow(16, i);
}
if (c == 'B') {
result += 11 * Math.pow(16, i);
}
if (c == 'C') {
result += 12 * Math.pow(16, i);
}
if (c == 'D') {
result += 13 * Math.pow(16, i);
}
if (c == 'E') {
result += 14 * Math.pow(16, i);
}
if (c == 'F') {
result += 15 * Math.pow(16, i);
}
}
}
System.out.println(result);
}
} public static void main(String[] args) {
try(Scanner input = new Scanner(System.in)){
while (input.hasNextLine()) {
String str = input.nextLine();
if(str.contains("0x") || str.contains("0X")){
str = str.replaceAll("^0[xX]", "").trim();
}
BigInteger integer = new BigInteger(str, 16);
System.out.println(integer);
}
}
} public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
char[] strChar = in.nextLine().toCharArray();
Map<Character,Integer> abcdefMap = new HashMap<>();
abcdefMap.put('A',10);
abcdefMap.put('B',11);
abcdefMap.put('C',12);
abcdefMap.put('D',13);
abcdefMap.put('E',14);
abcdefMap.put('F',15);
double result = 0;
int cimi = 0;
for(int i=strChar.length-1;i>=0;i--){
if(String.valueOf(strChar[i]).equals(String.valueOf(0))
|| 'x'==strChar[i]) continue;
int shuzi = abcdefMap.get(strChar[i])==null?Character.getNumericValue(strChar[i]):abcdefMap.get(strChar[i]);
result = result + shuzi*(Math.pow(16,cimi));
cimi++;
}
System.out.println((long)result);
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = in.nextLine();
a = a.substring(2, a.length());
int newa = Integer.parseInt(a, 16);
System.out.println(newa);
}
} private static Map<Character,Integer> map = new HashMap<>();
static {
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 scanner = new Scanner(System.in);
while(scanner.hasNext()){
String string = scanner.nextLine();
// 排除掉前边的0x(代表16进制)
String data = string.substring(2,string.length());
char[] charArray = data.toCharArray();
int n = 0;
int sum = 0;
for(int i = charArray.length-1;i>=0;i--){
// 这里减0目的是为了获取整数值
sum = sum + map.getOrDefault(charArray[i],charArray[i]-'0') * (int) Math.pow(16,n);
n++;
}
System.out.println(sum);
}
scanner.close();
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.next();
char[] chars = str.toCharArray();
int res = 0,x;
for(int i = 2; i < chars.length; i++){
char c = chars[i];
if(c <= 'F' && c >= 'A'){
x = c - 'A' + 10;
}else{
x = c - '0';
}
res = res * 16 + x;
}
System.out.println(res);
}
} import java.util.Scanner;
import java.lang.Integer;//因为之后要用到Integer的方法
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String hexNum = in.nextLine();//因为只输入一个数且用\n结尾,所以将next方法改为nextLine
hexNum = hexNum.substring(2);//字符串截取,即只要0x之后的内容
int decimalNum = Integer.parseInt(hexNum,16);//此时hexNum是字符串,利用Integer的paseInt方法实现进制转换,16代表16进制
System.out.println(decimalNum);
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
Integer i = Integer.parseInt(in.nextLine().substring(2),16);
System.out.println(i);
}
} package org.huawei.hwoj.induction; /* * HJ5 进制转换 * 写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。 * 数据范围:保证结果在 1 ≤ 𝑛 ≤ 231−1 * * 输入描述: * 输入一个十六进制的数值字符串。 * 输出描述: * 输出该数值的十进制字符串。不同组的测试用例用\n隔开。 * * 示例1 * 输入:0xAA 输出:170 输入:0xAF 输出:175 输入:0xFF 输出:255 * */ import java.util.Scanner; public class DecimalConversion { //解题思路:套用进制转换的公式,即第n位(从低位开始)数字乘以进制的n-1次幂,加和 public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { //读入十六进制的数值字符串 String s = sc.next(); //转换后的结果值 int result = 0; //由于前面两位是'0x',故从第三位开始 for (int i = 0; i < s.length() - 2; i++) { char c = s.charAt(i + 2); //记录字母转换成的数值,字母'A-F'、'a-f'对应数字10-15 int temp = 0; if (c >= '0' && c <= 9) { temp = c - '0'; } else if (c >= 'A' && c <= 'F') { temp = c - 'A' + 10; } else if (c >= 'a' && c <= 'f') { temp = c - 'a' + 10; } //累积求和:16进制的n-1次方 result += temp * Math.pow(16, s.length() - i - 3); } System.out.println(result); } } }