英文:
Hexadecimal to binary and decimal converter
问题
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: ");
    }
}
英文:
everyone. Here I have a hexa to binary and decimal converter. The problem is when I enter an invalid input such as the letter G or X it gives me a negative output. How can I prevent it from doing this and instead print out that the it is an invalid number
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: ");
}
} 
</details>
# 答案1
**得分**: 1
尝试这段代码。你只需要检查 `charAt` 是否返回一个正值,如果返回 -1,意味着你要查找的字符不在该字符串中。
```java
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) {
/* 首先将十六进制转换为十进制 */
decnum = hex2decimal(hexdecnum);
if (decnum == -1)
System.out.println("Incorrect Hex Value");
else {
System.out.print("Equivalent Dec Number is : " + decnum);
System.out.println();
/* 现在将十进制转换为二进制 */
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: ");
}
}
英文:
Try this code. You just need to check if the charAt returns a positive value if it returns -1 that means the character you are looking for is not in that string.
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
得分: 1
根据您的评论,我已经更新了程序,允许仅接受在十六进制范围为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
如果有任何疑问/问题,请随时进行评论。
英文:
Based on your comment, I have updated the program to allow accepting a number only in the hexadecimal range of 90 to FF.
Do it as follows:
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);
}
}
A sample run:
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
Another sample run:
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
Another sample run:
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
Another sample run:
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
Feel free to comment in case of any doubt/issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论