Question

2 Write a recursive method that parses a hex number as a string into a decimal integer. The method header is: public static i

This needs too be in java programming language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Recursive program is Java To convert Hex to Decimal

Code is

import java.util.*;
public class Main
{

public static int hex2Dec(String hexString) {
int decimal = 0;
// declare hex sequence in a string
String hexCode = "0123456789ABCDEF";
// convert input string to uppercase
hexString = hexString.toUpperCase();
int length = hexString.length();
if (length > 0) {
// check for first char on the input hexString
char ch = hexString.charAt(0);
int digit = hexCode.indexOf(ch);
String substring = hexString.substring(1);
// Now calculate the Decimal for that Hex Character
decimal = digit * (int) Math.pow(16, length - 1) + hex2Dec(substring);
}
return decimal;
}
public static void main (String[] args) {
Scanner scn=new Scanner(System.in);
System.out.println("Enter the Hexadecimal String ");
String str=scn.next();

// Print the result
System.out.println("The Decimal Value is "+hex2Dec(str));
}

}

Code Screen And Outputs

8 9 10 12 Main.java 1 import java.util.*; 2 public class Main 3-{ 4 5 public static int hex2Dec(string hexString) { 6 int decEnter the Hexadecimal String BabA73 The Decimal value is 12237427 ...Program finished with exit code 0 Press ENTER to exit coEnter the Hexadecimal String BAC98 The Decimal value is 765080 ... Program finished with exit code o Press ENTER to exit cons

This is how you can write the program for Hex2Dec using recursion in Java

If u like the answer please Give It a Thumbs Up and have any doubt comment it

Add a comment
Know the answer?
Add Answer to:
This needs too be in java programming language 2 Write a recursive method that parses a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT