# Java Convert Hexadecimal to Decimal

In Java, you can convert a hexadecimal string to decimal using the `Integer.parseInt()` method by passing the hexadecimal string and the radix value 16 as arguments. Here’s an example:

```String hex = "1A"; // hexadecimal string
int decimal = Integer.parseInt(hex, 16); // convert hexadecimal to decimal
System.out.println(decimal); // prints "26"
```

In this example, the `hex` variable contains the hexadecimal string “1A”. The `Integer.parseInt()` method is called with the `hex` string and the radix value 16 as arguments, which tells Java to interpret the string as a hexadecimal number. The method returns an integer value, which is then stored in the `decimal` variable. Finally, the decimal value is printed to the console using the `System.out.println()` method.

### Java Hexadecimal to Decimal conversion: Integer.parseInt()

Yes, in Java, you can use the `Integer.parseInt()` method to convert a hexadecimal string to a decimal integer value.

The `parseInt()` method takes two arguments: the string to be parsed and the radix or base of the number system used in the string. In the case of a hexadecimal string, the radix should be 16.

Here’s an example of how to use `Integer.parseInt()` to convert a hexadecimal string to a decimal integer:

```String hexString = "4D2"; // hexadecimal string
int decimalInt = Integer.parseInt(hexString, 16); // convert hexadecimal to decimal
System.out.println(decimalInt); // prints "1234"
```

In this example, we have a hexadecimal string “4D2”. We pass this string as the first argument to `parseInt()`, and 16 as the second argument to specify that we want to parse it as a hexadecimal number. The `parseInt()` method returns an integer value, which is then assigned to the `decimalInt` variable. Finally, we print the decimal integer value using the `System.out.println()` method.

### Java Hexadecimal to Decimal conversion: Custom Logic

In Java, you can also implement a custom logic to convert a hexadecimal string to a decimal integer value. Here’s an example of how to do that:

```public static int hexToDecimal(String hexString) {
int decimal = 0;
String hexDigits = "0123456789ABCDEF";
hexString = hexString.toUpperCase();

for (int i = 0; i < hexString.length(); i++) {
char c = hexString.charAt(i);
int d = hexDigits.indexOf(c);
decimal = 16 * decimal + d;
}

return decimal;
}
```

n this example, we have defined a static method `hexToDecimal()` that takes a hexadecimal string as an argument and returns the equivalent decimal integer value.

Inside the method, we first initialize a `decimal` variable to 0. We also define a `hexDigits` string that contains the characters used in the hexadecimal number system. We convert the input hexadecimal string to uppercase using the `toUpperCase()` method to handle both lowercase and uppercase input strings.

We then loop through each character in the hexadecimal string and use the `indexOf()` method of `hexDigits` to find its corresponding decimal value. We then multiply the current decimal value by 16 and add the decimal value of the current hexadecimal digit to the running total.

Finally, we return the resulting decimal integer value.

Here’s an example of how to use the `hexToDecimal()` method:

```String hexString = "4D2"; // hexadecimal string
int decimalInt = hexToDecimal(hexString); // convert hexadecimal to decimal using custom logic
System.out.println(decimalInt); // prints "1234"
```

In this example, we pass the hexadecimal string “4D2” as an argument to the `hexToDecimal()` method, which returns the decimal integer value 1234. We then print this value to the console using the `System.out.println()` method.