HexString转Decimal(Int)异常:输入任意值结果均为0或1求解
十六进制字符串转十进制整数的问题修复
我编写了三种基于数组实现十六进制字符串转十进制整数的Java代码,分别为if/else判断、字符串switch、字符字面量switch版本。核心逻辑是将十六进制字符串拆分为数组后从右到左遍历计算,但无论输入什么十六进制字符串,输出结果始终为0或1。先解决功能问题,代码冗长低效的问题后续再优化。
v1 - if/else测试版本(原代码)
System.out.println("Enter a hexadecimal String to convert to decimal (base_10)"); while(true) { System.out.print("Hexadecimal >> "); String hex = scan.nextLine(); String hexArray[] = hex.split(""); //int hexNum[] = new int[hexArray.length]; //System.out.println(Arrays.toString(hexArray)); //test int sum = 0; int exp = 0; for (int i = hexArray.length-1; i > 0; i--) { if(hexArray[i].equalsIgnoreCase("A")) sum += 10 * Math.pow(16,exp); else if(hexArray[i].equalsIgnoreCase("B")) sum += 11 * Math.pow(16,exp); else if(hexArray[i].equalsIgnoreCase("C")) sum += 12 * Math.pow(16,exp); else if(hexArray[i].equalsIgnoreCase("D")) sum += 13 * Math.pow(16,exp); else if(hexArray[i].equalsIgnoreCase("E")) sum += 14 * Math.pow(16,exp); else if(hexArray[i].equalsIgnoreCase("F")) sum += 15 * Math.pow(16,exp); else sum += (Integer.parseInt(hexArray[i]) * Math.pow(16,exp)); exp++; } System.out.println("\nHexadecimal: " + hex + "\t Decimal: " + sum); }
问题点与修复
- 循环条件错误:
i > 0会跳过数组索引0的字符(十六进制字符串的最高位),导致只计算了低位部分,修改为i >= 0才能遍历所有字符。 - Math.pow精度问题:
Math.pow返回double类型,转换为int时可能出现精度丢失,改用整数累乘的方式替代。
修复后的代码:
import java.util.Scanner; public class HexToDecimalIfElse { public static void main(String[] args) { System.out.println("Enter a hexadecimal String to convert to decimal (base_10)"); Scanner scan = new Scanner(System.in); while(true) { System.out.print("Hexadecimal >> "); String hex = scan.nextLine().trim(); if(hex.isEmpty()) continue; String hexArray[] = hex.split(""); int sum = 0; int multiplier = 1; // 替代Math.pow,初始为16^0=1 for (int i = hexArray.length-1; i >= 0; i--) { String ch = hexArray[i]; int value; if(ch.equalsIgnoreCase("A")) value = 10; else if(ch.equalsIgnoreCase("B")) value = 11; else if(ch.equalsIgnoreCase("C")) value = 12; else if(ch.equalsIgnoreCase("D")) value = 13; else if(ch.equalsIgnoreCase("E")) value = 14; else if(ch.equalsIgnoreCase("F")) value = 15; else value = Integer.parseInt(ch); sum += value * multiplier; multiplier *= 16; // 每次循环乘以16,对应下一位的权重 } System.out.println("\nHexadecimal: " + hex + "\t Decimal: " + sum); } } }
v2 - 字符串switch实现版本(原代码)
System.out.println("Enter a hexadecimal String to convert to decimal (base_10)"); while(true) { System.out.print("Hexadecimal >> "); String hex = scan.nextLine(); String[] hexArray = hex.split(""); System.out.println(Arrays.toString(hexArray)); //test int sum = 0; int exp = 0; for (int i = hexArray.length-1; i > 0; i--) { String s = hexArray[i]; switch(s) { case "A": case "a": sum += (10 * Math.pow(16,exp)); break; case "B": case "b": sum += (11 * Math.pow(16,exp)); break; case "C": case "c": sum += (12 * Math.pow(16,exp)); break; case "D": case "d": sum += (13 * Math.pow(16,exp)); break; case "E": case "e": sum += (14 * Math.pow(16,exp)); break; case "F": case "f": System.out.println("hi"); sum += (15 * Math.pow(16,exp)); break; default: //i.e. NUMBERS sum += ((Integer.parseInt(hexArray[i]) * Math.pow(16,exp))); } exp++; } System.out.println("\nHexadecimal: " + hex + "\t Decimal: " + sum); System.out.println("\n"); }
问题点与修复
同v1的两个核心问题:循环条件跳过首位、Math.pow精度问题,修复如下:
import java.util.Arrays; import java.util.Scanner; public class HexToDecimalStringSwitch { public static void main(String[] args) { System.out.println("Enter a hexadecimal String to convert to decimal (base_10)"); Scanner scan = new Scanner(System.in); while(true) { System.out.print("Hexadecimal >> "); String hex = scan.nextLine().trim(); if(hex.isEmpty()) continue; String[] hexArray = hex.split(""); System.out.println(Arrays.toString(hexArray)); //test int sum = 0; int multiplier = 1; for (int i = hexArray.length-1; i >= 0; i--) { String s = hexArray[i]; int value = 0; switch(s) { case "A": case "a": value = 10; break; case "B": case "b": value = 11; break; case "C": case "c": value = 12; break; case "D": case "d": value = 13; break; case "E": case "e": value = 14; break; case "F": case "f": System.out.println("hi"); value = 15; break; default: //i.e. NUMBERS value = Integer.parseInt(hexArray[i]); } sum += value * multiplier; multiplier *= 16; } System.out.println("\nHexadecimal: " + hex + "\t Decimal: " + sum); System.out.println("\n"); } } }
v3 - 字符字面量switch实现版本(原代码)
System.out.println("Enter a hexadecimal String to convert to decimal (base_10)"); while(true) { System.out.print("Hexadecimal >> "); String hex = scan.nextLine(); String[] hexArray = hex.split(""); System.out.println(Arrays.toString(hexArray)); //test int sum = 0; int exp = 0; for (int i = hexArray.length-1; i > 0; i--) { char c = hexArray[i].charAt(0); switch(c) { case '0': break; case '1': sum += Math.pow(16,exp); break; case '2': sum += 2 * Math.pow(16,exp); break; case '3': sum += 3 * Math.pow(16,exp); break; case '4': sum += 4 * Math.pow(16,exp); break; case '5': sum += 5 * Math.pow(16,exp); break; case '6': sum += 6 * Math.pow(16,exp); break; case '7': sum += 7 * Math.pow(16,exp); break; case '8': sum += 8 * Math.pow(16,exp); break; case '9': sum += 9 * Math.pow(16,exp); break; case 'A': case 'a': sum += 10 * Math.pow(16,exp); break; case 'B': case 'b': sum += 11 * Math.pow(16,exp); break; case 'C': case 'c': sum += 12 * Math.pow(16,exp); break; case 'D': case 'd': sum += 13 * Math.pow(16,exp); break; case 'E': case 'e': sum += 14 * Math.pow(16,exp); break; case 'F': case 'f': sum += 15 * Math.pow(16,exp); break; default: ; //Invalid Character } exp++; } System.out.println("\nHexadecimal: " + hex + "\t Decimal: " + sum); }
问题点与修复
除循环条件和Math.pow的问题外,原代码中case '0'未处理数值累加(逻辑不完整),修复后:
import java.util.Arrays; import java.util.Scanner; public class HexToDecimalCharSwitch { public static void main(String[] args) { System.out.println("Enter a hexadecimal String to convert to decimal (base_10)"); Scanner scan = new Scanner(System.in); while(true) { System.out.print("Hexadecimal >> "); String hex = scan.nextLine().trim(); if(hex.isEmpty()) continue; String[] hexArray = hex.split(""); System.out.println(Arrays.toString(hexArray)); //test int sum = 0; int multiplier = 1; for (int i = hexArray.length-1; i >= 0; i--) { char c = hexArray[i].charAt(0); int value = 0; switch(c) { case '0': value = 0; break; case '1': value = 1; break; case '2': value = 2; break; case '3': value = 3; break; case '4': value = 4; break; case '5': value = 5; break; case '6': value = 6; break; case '7': value = 7; break; case '8': value = 8; break; case '9': value = 9; break; case 'A': case 'a': value = 10; break; case 'B': case 'b': value = 11; break; case 'C': case 'c': value = 12; break; case 'D': case 'd': value = 13; break; case 'E': case 'e': value = 14; break; case 'F': case 'f': value = 15; break; default: value = 0; // 无效字符按0处理,也可抛出异常 } sum += value * multiplier; multiplier *= 16; } System.out.println("\nHexadecimal: " + hex + "\t Decimal: " + sum); } } }
内容的提问来源于stack exchange,提问作者Skelefish
相关产品推荐
相关产品推荐

