Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Program {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.println("Enter a hex number");
- String hex=input.nextLine();
- int sum=0;
- for (int i=hex.length()-1; i>=0; i--){
- int dec=0;
- char hexChar=hex.charAt((hex.length()-1)-i);
- System.out.println(hexChar);
- if (hexChar>= 'A' && hexChar<='F'){
- dec=10+hexChar-'A';
- }
- else{
- dec=hexChar-'0';}
- System.out.println(dec);
- sum=(int) (sum+Math.pow(16.0,i)*dec);
- }
- System.out.print(sum);
- }
- }
Add Comment
Please, Sign In to add comment