I need to make a program that can convert hexadecimal values to decimal. I got the program to work but I need to make my program display the same output no matter if the hexadecimal value entered contains the "0x" in front or not.
this is my code.
import java.util.Scanner;
public class Main {
public static long hex2decimal(String s){
String digits = "0123456789ABCDEF";
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) {
Scanner sc = new Scanner(System.in);
long decimal;
System.out.println("Please enter the hex string:");
String s = sc.next().toUpperCase();
decimal = hex2decimal(s);
System.out.println(decimal);
}
}