Java examples for Algorithm:Number
Hex To Decimal
import java.util.Scanner; public class Main { public static void main(String[] args) { String hex = "A"; int number = Calculate(hex); String binary = CalculateToBinary(number); System.out.println(hex);//from w w w. jav a 2 s. c om System.out.println(number); System.out.println(binary); } public static int Calculate(String hex) { int number = 0; int power = 1; for (int i = hex.length() - 1; i >= 0; i--) { int num; switch (hex.charAt(i)) { case 'A': num = 10; break; case 'B': num = 11; break; case 'C': num = 12; break; case 'D': num = 13; break; case 'E': num = 14; break; case 'F': num = 15; break; default: num = (int) hex.charAt(i) - 48; break; } number = number + (num * power); power = power * 16; } return number; } public static String CalculateToBinary(int number) { String bin = ""; int bit = 0; while (number > 0) { bit = number % 2; number = number / 2; bin = bit + bin; } return bin; } }