开发者社区> 问答> 正文

十六进制到二进制和十进制转换器

大家。在这里,我有一个六进制到二进制和十进制转换器。问题是当我输入无效的输入(例如字母G或X)时,输出为负。我如何防止它这样做,而是打印出它是无效数字

public static int hex2decimal(String s)
        {
                 String digits = "0123456789ABCDEF";
                 s = s.toUpperCase();
                 int val = 0;
                 for (int i = 0; i < s.length(); i++)
                 {
                     char c = s.charAt(i);
                     int d = digits.indexOf(c);
                     val = 16*val + d;
                 }
                 return val;
        }

         public static void main(String args[])
            {
                String hexdecnum;
                int decnum, i=1, j;

                int binnum[] = new int[100];
                Scanner scan = new Scanner(System.in);

                System.out.print("Enter Hexadecimal Number : ");
                hexdecnum = scan.nextLine();
                final int MAX_LENGTH = 2;

                      if(String.valueOf(hexdecnum).length() <= MAX_LENGTH) {
                          /* first convert the hexadecimal to decimal */

                        decnum = hex2decimal(hexdecnum);
                        System.out.print("Equivalent Dec Number is : "+ decnum);
                        System.out.println();

                        /* now convert the decimal to binary */

                        while(decnum != 0)
                        {
                            binnum[i++] = decnum%2;
                            decnum = decnum/2;
                        }

                        System.out.print("Equivalent Binary Number is : ");
                        for(j=i-1; j>0; j--)
                        {
                            System.out.print(binnum[j]);
                        }
                      } else {
                        System.out.println("ERROR: Invalid Input");
                        System.out.print("Enter a number: ");
                      }
                    } 

问题来源:Stack Overflow

展开
收起
montos 2020-03-22 08:43:32 1282 0
2 条回答
写回答
取消 提交回答
  • 1.PNG

    2020-03-22 13:52:13
    赞同 展开评论 打赏
  • 仅接受90到FF的十六进制范围内的数字。

    import java.util.Scanner;
    
    public class Main {
        public static int hex2decimal(String s) {
            String digits = "0123456789ABCDEF";
            s = s.toUpperCase();
            int val = 0;
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                int d = digits.indexOf(c);
                val = 16 * val + d;
            }
            return val;
        }
    
        public static void main(String args[]) {
            String hexdecnum;
            int decnum, i = 1, j;
    
            int binnum[] = new int[100];
            Scanner scan = new Scanner(System.in);
            boolean valid;
            do {
                valid = true;
                System.out.print("Enter Hexadecimal number in the range of 90 to FF: ");
                hexdecnum = scan.nextLine();
                final int MAX_LENGTH = 2;
    
                if (hexdecnum.matches("[A-Fa-f0-9]{2}") && hex2decimal(hexdecnum) >= 144) {
                    /* first convert the hexadecimal to decimal */
    
                    decnum = hex2decimal(hexdecnum);
                    System.out.print("Equivalent Dec Number is : " + decnum);
                    System.out.println();
    
                    /* now convert the decimal to binary */
    
                    while (decnum != 0) {
                        binnum[i++] = decnum % 2;
                        decnum = decnum / 2;
                    }
    
                    System.out.print("Equivalent Binary Number is : ");
                    for (j = i - 1; j > 0; j--) {
                        System.out.print(binnum[j]);
                    }
                } else {
                    System.out.println("ERROR: Invalid Input");
                    valid = false;
                }
            } while (!valid);
        }
    }
    

    运行示例:

    Enter Hexadecimal number in the range of 90 to FF: abc
    ERROR: Invalid Input
    Enter Hexadecimal number in the range of 90 to FF: ab
    Equivalent Dec Number is : 171
    Equivalent Binary Number is : 10101011
    

    另一个示例运行:

    Enter Hexadecimal number in the range of 90 to FF: AG
    ERROR: Invalid Input
    Enter Hexadecimal number in the range of 90 to FF: AB
    Equivalent Dec Number is : 171
    Equivalent Binary Number is : 10101011
    

    另一个示例运行:

    Enter Hexadecimal number in the range of 90 to FF: 21
    ERROR: Invalid Input
    Enter Hexadecimal number in the range of 90 to FF: 90
    Equivalent Dec Number is : 144
    Equivalent Binary Number is : 10010000
    

    另一个示例运行:

    Enter Hexadecimal number in the range of 90 to FF: 40
    ERROR: Invalid Input
    Enter Hexadecimal number in the range of 90 to FF: FF
    Equivalent Dec Number is : 255
    Equivalent Binary Number is : 11111111
    

    回答来源:Stack Overflow

    2020-03-22 08:45:31
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载