Vigesimal in Java
A number system with a base of 20 is known as the vigesimal in Java. A base-20 (base-score) numeric system, often known as a vigesimal system, is centered on the twenty (in the same way in which the system of the decimal numeral is based on the ten). The word vicesimal, which means "twentieth," is a derivative of Latin. Similar to Duodecimal values, we can determine the corresponding Vigesimal value of binary, octal, decimal, and hexadecimal numbers using preset or user-defined algorithms, or we may determine a corresponding Vigesimal number of other base numbers for a given Vigesimal number. The base 20 vigesimal (used by the Mayans) and base 60 sexagesimal (used by the Babylonians) number systems, as well as the base 12 duodecimal (used by the Egyptians) number system, were all utilized in ancient times.
A few illustrations of vigesimal numbers are shown here.
- The binary value (11010111)2 corresponds to the vigesimal number (AF)20.
- The octal number (362)8 equals to the vigesimal number (C2)20.
- The hexadecimal value (A563E)16 is the same as the vigesimal number (44DBI)20.
- Hexa-trigesimal number (A563E)32 is equal to the vigesimal number (36BJCE)20.
How to find a vigesimal number
The steps to determine the vigesimal number of a given hexadecimal number in any base are as follows:
- Consider the user's input as a hexadecimal number.
- To convert it to decimal, create a user-defined method.
- We also develop a user-defined function which thus accepts a decimal integer and changes it into a vigesimal.
- return the digits of the vigesimal number.
Let's put the logic for changing any hexadecimal value into a vibrational number into practice:
Let the name of the file is VigesimalNumberDemo.java
package kamalakar.com.VigesimalNumber;
import the necessary classes and packages.
import java.util.*;
// For the purpose of determining the corresponding vigesimal number of a given hexadecimal number, define the class VigesimalNumberDemo.
class VigesimalNumberDemo {
// The convertToDeci() method should be developed to return the specified hexadecimal number's decimal equivalent.
public static String convertToDeci(String values, int base, Map<Char, Integers>hexatoDeci)
{
int sum = 0;
int pow = 0;
String tempData = values;
//logic to find equivalent decimal number
for (int s = tempData.length() - 1; s >= 0; s--) {
int valu = tempData.charAt(s) - '0';
if (base == 16 && hexatoDeci.containsKey(tempData.charAt(i))) {
val = hexatoDeci.get(tempData.charAt(i));
}
sums += (valu) * (Math.power(base, power++));
}
return String.valueOfthe(sums);
}
// Convert a decimal number to a vigesimal number and return the result to the main() method by creating the convertToVigesimal() method.
public static String convertToVigesimal(String values, int base, Map<Integers, Char>decitoHexa, Map<Char, Integers>hexatoDeci)
{
String valu = values;
int newBase = base;
// Check to see if the base is decimal.
if (newBase != 10) {
// The convertToDeci() method is used if the base is not given as 10, and it returns the specified number value corresponding decimal representation.
valu = convertToDeci(values, base, hexatoDeci);
// We modify the value of newBase to 10 after the conversion of the number.
newBase = 10;
}
// string to integer conversion of the given number.
int temp = Integers.parseInt(valu);
int rem;
String isvigesimal = "";
// In order to define the characters, build the vigesimalChar[] array.
char vigesimalChar[] = {'A','B','C','D','E','F','G','H','I','J', 'K', '0','1','2','3','4','5','6','7','8','9'};
// finding an equivalent vigesimal number using the concept of reasoning
while(temp > 0)
{
rem = temp % 20;
vigesimal = vigesimalChar[rem]+vigesimal;
temp = temp / 20;
}
return vigesimal;
}
//the main() method will be initiated here
public static void main(String[] args)
{
// a variable to hold hexadecimal numbers should be created
String valu;
// To receive input from the user, construct a scanner class object.
Scanner sc7 = new Scanner(System.in);
System.out.print("The hexadecimal number to be entered is:");
valu = sc7.nextLine();
// object of the scanner class is closed
Sc7.close();
// hexatoDeci and decitoHexa should be made to store the values.
Map<Char, Integers> hexatoDeci = new HashMap<>();
Map<Integers, Char> decitoHexa = new HashMap<>();
// mechanism for storing date in hexatoDeci and decitoHexa map
for (int s = 0; s < 6; s++) {
decitoHexa.put(10 + s, (char)('C' + s));
hexatoDec.put((char)('C' + s), 10 + s);
}
// Call convertToVigesimal(), then print the result of the call.
System.out.println("The Vigesimal number : " + convertToVigesimal(valu, 16, decitoHexa, hexatoDeci));
}
}
Output:
The hexadecimal number to be entered is : C563E
The Vigesimal number : 44DBI
Other user-defined methods must be developed in order to convert base numbers other than hexadecimal, such as the convertToDeci() method, which also will convert the supplied integer in to the decimal and return it for use in computing the vigesimal number.