Movatterモバイル変換


[0]ホーム

URL:


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

Commit9ee9612

Browse files
committed
Take into consideration hexadecimal numbers with floating point
1 parentfd24ffc commit9ee9612

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

‎src/main/java/com/conversions/HexadecimalToDecimal.java‎

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,58 @@ public class HexadecimalToDecimal {
1111
*/
1212
publicStringhexToDecimal(StringhexaStr) {
1313
StringhexaNumbers ="0123456789ABCDEF";
14-
intm,result =0;
14+
intm,result =0,decimalNumberBefore =0,power = -1;
15+
DoubledecimalNumberAfter =0.0;
1516
charletter;
1617
StringdecimalStr;
1718
hexaStr =hexaStr.toUpperCase();
18-
19-
for (inti =0 ;i <hexaStr.length() ;i++) {
20-
/**
21-
* Letter will store the hexadecimal number as long as we loop through
22-
* the string
23-
*/
24-
letter =hexaStr.charAt(i);
19+
intpointPosition =hexaStr.indexOf(".");
20+
/**
21+
* Check whether the number contains a float point or not
22+
*/
23+
if (pointPosition == -1) {
24+
for (inti =0 ;i <hexaStr.length() ;i++) {
25+
/**
26+
* Letter will store the hexadecimal number as long as we loop through
27+
* the string
28+
*/
29+
letter =hexaStr.charAt(i);
2530

26-
/**
27-
* m is the index of the number that we are looping through in the
28-
* hexaNumbers
29-
*/
30-
m =hexaNumbers.indexOf(letter);
31-
result =16*result +m;
32-
}
31+
/**
32+
* m is the index of the number that we are looping through in the
33+
* hexaNumbers
34+
*/
35+
m =hexaNumbers.indexOf(letter);
36+
result =16*result +m;
37+
}
38+
decimalStr =String.valueOf(result);
39+
40+
}
41+
else {
42+
for (inti =0 ;i <pointPosition ;i++) {
43+
letter =hexaStr.charAt(i);
44+
m =hexaNumbers.indexOf(letter);
45+
decimalNumberBefore =16*decimalNumberBefore +m;
46+
}
47+
48+
StringdecimalNumberBeforeStr =String.valueOf(decimalNumberBefore);
49+
50+
for (inti =pointPosition+1 ;i <hexaStr.length() ;i++) {
51+
letter =hexaStr.charAt(i);
52+
m =hexaNumbers.indexOf(letter);
53+
decimalNumberAfter = (decimalNumberAfter + (Math.pow(16,power))*m);
54+
power =power-1;
55+
}
56+
/**
57+
* Retrieve the decimal part of the result
58+
*/
59+
StringdecimalNumberAfterStr =String.valueOf(decimalNumberAfter);
60+
intindexOfDecimal =decimalNumberAfterStr.indexOf(".");
61+
decimalNumberAfterStr =decimalNumberAfterStr.substring(indexOfDecimal);
62+
63+
decimalStr =decimalNumberBeforeStr +decimalNumberAfterStr;
64+
}
3365

34-
decimalStr =String.valueOf(result);
35-
returndecimalStr ;
66+
returndecimalStr ;
3667
}
3768
}

‎src/test/java/com/conversions/HexadecimalToDecimalTest.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class HexadecimalToDecimalTest {
1010
voidtestHexadecimalToDecimalTest() {
1111

1212
HexadecimalToDecimalhexadecimalToDecimal =newHexadecimalToDecimal();
13+
14+
//HexadecimaltTesting
1315
Assertions.assertEquals("171",hexadecimalToDecimal.hexToDecimal("AB"),"Incorrect Conversion");
1416
Assertions.assertEquals("5680077",hexadecimalToDecimal.hexToDecimal("56ABCD"),"Incorrect Conversion");
1517
Assertions.assertEquals("5174921",hexadecimalToDecimal.hexToDecimal("4ef689"),"Incorrect Conversion");
@@ -18,6 +20,13 @@ void testHexadecimalToDecimalTest() {
1820
//It returns -1 if you enter a wrong hexaDecimal
1921
Assertions.assertEquals("-1",hexadecimalToDecimal.hexToDecimal("K"),"Incorrect Conversion");
2022

23+
//Hexadecimal with floating point testing
24+
Assertions.assertEquals("10.6875",hexadecimalToDecimal.hexToDecimal("A.B"),"Incorrect Conversion");
25+
Assertions.assertEquals("1386.737548828125",hexadecimalToDecimal.hexToDecimal("56A.BCD"),"Incorrect Conversion");
26+
Assertions.assertEquals("78.9630279541015625",hexadecimalToDecimal.hexToDecimal("4e.f689"),"Incorrect Conversion");
27+
Assertions.assertEquals("0.93359375",hexadecimalToDecimal.hexToDecimal(".EF"),"Incorrect Conversion");
28+
Assertions.assertEquals("171.8044281005859375",hexadecimalToDecimal.hexToDecimal("AB.CDEF"),"Incorrect Conversion");
29+
2130
}
2231

2332
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp