Posted: Wed Nov 22, 2017 12:21 am Post subject: I'm not getting the desired output after converting
Guest
I executed the code below in my Eclipse. After execution i'm not getting the desired output.
Code:
import java.util.Scanner;
public class HexaToDecimal
{
public static int convertHexadecimal(String str)
{
String strHexa = "0123456789ABCDEF";
str = str.toUpperCase();
int temp = 0;
for (int a = 0; a < str.length(); a++)
{
char h = str.charAt(a);
int d = strHexa.indexOf(h);
temp = 16 * temp + d;
}
return temp;
}
public static void main(String[] args)
{
String strHexdecimal;
int decimalNumber;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter hexadecimal number : ");
strHexdecimal = sc.nextLine();
decimalNumber = convertHexadecimal(strHexdecimal);
System.out.print("The decimal number is " + strHexdecimal);
sc.close();
}
}
This may be working fine for numbers. Think of it however just as an incentive to code your own when moving from a byte[] to hex text and vice versa... _________________ MQ & Broker admin
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum