【问题标题】:Hexadecimal to binary and decimal converter十六进制到二进制和十进制转换器
【发布时间】:2020-06-27 22:13:26
【问题描述】:

每个人。在这里,我有一个六进制到二进制和十进制转换器。问题是当我输入无效输入(例如字母 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: ");
                      }
                    } 

【问题讨论】:

  • 你能检查一下输入是否只包含 A-F 吗?

标签: java eclipse binary hex


【解决方案1】:

试试这个代码。您只需要检查 charAt 是否返回正值,如果它返回 -1 表示您要查找的字符不在该字符串中。


    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);
            if (d!=-1)
                val = 16*val + d;
            else
                return 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);
            if (decnum==-1)
                System.out.println("Incorrect Hex Value");
            else {
                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: ");
        }
    }


【讨论】:

    【解决方案2】:

    根据您的评论,我已更新程序以允许仅接受 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
    

    如有任何疑问/问题,请随时发表评论。

    【讨论】:

    • 你好,非常感谢。如何添加最小值和最大值?例如:用户应输入的最小数字是 90(十进制等效 144),最大数字是 FF(十进制等效 255)。
    • 不客气。我也更新了我的答案以满足这个要求。
    • 你能解释一下你对范围部分做了什么吗?
    • 我已经为范围部分设置了条件if (hexdecnum.matches("[A-Fa-f0-9]{2}") &amp;&amp; Integer.parseInt(hexdecnum, 16) &gt;= 144)。它将数字限制为仅限2 数字,由来自0 to 9 and A to F&gt;= 144&lt;=FF 的数字组成。请注意,&lt;=FF 将被检查的第一部分隐式覆盖(即,将数字限制为仅限 2 数字,并由来自 0 to 9 and A to F 的数字组成)。
    • 你是否使用内置函数将十六进制转换为十进制
    猜你喜欢
    • 2014-10-30
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 2016-02-09
    • 2012-06-26
    • 2011-10-11
    • 1970-01-01
    相关资源
    最近更新 更多