Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8400a6e

Browse files
Convert Hexadecimal to Decimal
1 parent b4de0af commit 8400a6e

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.java.convertor;
2+
3+
/*
4+
* Hexadecimal To Decimal Conversion
5+
* -------------------------------
6+
*
7+
* Hexadecimal::
8+
* In mathematics and computing, the hexadecimal
9+
* (also base 16 or hex) numeral system is a
10+
* positional numeral system that represents numbers
11+
* using a radix (base) of 16.
12+
*
13+
* Unlike the common way of representing numbers
14+
* using 10 symbols, hexadecimal uses 16 distinct symbols,
15+
* most often the symbols "0"–"9" to represent
16+
* values 0 to 9, and "A"–"F" (or alternatively "a"–"f")
17+
* to represent values 10 to 15.
18+
*
19+
* Steps
20+
* -----
21+
* - get input (hexadecimal) from the user
22+
* - create a variable, lets say pow (default value is 0)
23+
* - create a variable, lets say decimal (default value is 0)
24+
* - get the last digit of the hex number
25+
* - get the corresponding numeric value
26+
* - because hexadecimal digits contains alphabets also
27+
* {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
28+
* - multiply the numeric value with power of 16 to pow variable
29+
* - add the above result with
30+
* - increment the pow variable
31+
* - continue all the above steps for all the digits of hex
32+
* - read the digits from the reverse direction
33+
*
34+
* say hex = 153
35+
* decimal = 1 * 16^2 + 5 * 16^1 + 3 * 16^0
36+
* decimal = 1 * 256 + 5 * 16 + 3 * 1
37+
* decimal = 256 + 80 + 3
38+
* decimal = 339
39+
*
40+
* say hex = FAD
41+
* decimal = F * 16^2 + A * 16^1 + D * 16^0
42+
* decimal = 15*256 + 10*16 + 13*1
43+
* decimal = 3840 + 160 + 13
44+
* decimal = 4013
45+
*/
46+
47+
public class HexaToDecimal {
48+
public static void main(String[] args) {
49+
String hex = "35432";
50+
// String hex = "153";
51+
// String hex = "A0";
52+
// String hex = "FAD";
53+
54+
int decimal = 0;
55+
int pow = 0;
56+
57+
for(int i=hex.length()-1;i>=0;i--){
58+
char unitChar = hex.charAt(i);
59+
int unitDigitValue = numericValue(unitChar);
60+
decimal = (int) (decimal + unitDigitValue*Math.pow(16, pow));
61+
pow++;
62+
}
63+
64+
System.out.println("Hexadecimal Value is :"+hex);
65+
System.out.println("Decimal Value :"+decimal);
66+
}
67+
68+
private static int numericValue(char c){
69+
//hexa digit may contain alphabet also
70+
//so we need to it to the corresponding numeric value
71+
switch (c) {
72+
case '0':
73+
return 0;
74+
case '1':
75+
return 1;
76+
case '2':
77+
return 2;
78+
case '3':
79+
return 3;
80+
case '4':
81+
return 4;
82+
case '5':
83+
return 5;
84+
case '6':
85+
return 6;
86+
case '7':
87+
return 7;
88+
case '8':
89+
return 8;
90+
case '9':
91+
return 9;
92+
case 'a':
93+
case 'A':
94+
return 10;
95+
case 'b':
96+
case 'B':
97+
return 11;
98+
case 'c':
99+
case 'C':
100+
return 12;
101+
case 'd':
102+
case 'D':
103+
return 13;
104+
case 'e':
105+
case 'E':
106+
return 14;
107+
case 'f':
108+
case 'F':
109+
return 15;
110+
default:
111+
return 0;
112+
}
113+
}
114+
}
115+
/*
116+
OUTPUT
117+
Hexadecimal Value is :35432
118+
Decimal Value :218162
119+
120+
Hexadecimal Value is :A0
121+
Decimal Value :160
122+
123+
Hexadecimal Value is :153
124+
Decimal Value :339
125+
126+
Hexadecimal Value is :FAD
127+
Decimal Value :4013
128+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /