I have a homework assignment where I need to do three-way conversion between decimal, binary and hexadecimal. The function I need help with is converting a decimal into a he
Simple:
public static String decToHex(int dec)
{
return Integer.toHexString(dec);
}
As mentioned here: Java Convert integer to hex integer
I will use
Long a = Long.parseLong(cadenaFinal, 16 );
since there is some hex that can be larguer than intenger and it will throw an exception
A better solution to convert Decimal To HexaDecimal and this one is less complex
import java.util.Scanner;
public class DecimalToHexa
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Decimal number: ");
int n=sc.nextInt();
if(n<0)
{
System.out.println("Enter a positive integer");
return;
}
int i=0,d=0;
String hx="",h="";
while(n>0)
{
d=n%16;`enter code here`
n/=16;
if(d==10)h="A";
else if(d==11)h="B";
else if(d==12)h="C";
else if(d==13)h="D";
else if(d==14)h="E";
else if(d==15)h="F";
else h=""+d;
hx=""+h+hx;
}
System.out.println("Equivalent HEXA: "+hx);
}
}
Consider dec2m method below for conversion from dec to hex, oct or bin.
Sample output is
28 dec == 11100 bin
28 dec == 34 oct
28 dec == 1C hex
public class Conversion {
public static void main(String[] argv) {
int x = 28; // sample number
if (argv.length > 0)
x = Integer.parseInt(argv[0]); // number from command line
System.out.printf("%d dec == %s bin\n", i, dec2m(x, 2));
System.out.printf("%d dec == %s oct\n", i, dec2m(x, 8));
System.out.printf("%d dec == %s hex\n", i, dec2m(x, 16));
}
static String dec2m(int N, int m) {
String s = "";
for (int n = N; n > 0; n /= m) {
int r = n % m;
s = r < 10 ? r + s : (char) ('A' - 10 + r) + s;
}
return s;
}
}
I need a function that takes in an int dec and returns a String hex.
I found a more elegant solution from http://introcs.cs.princeton.edu/java/31datatype/Hex2Decimal.java.html . I changed a bit from the original ( see the edit )
// precondition: d is a nonnegative integer
public static String decimal2hex(int d) {
String digits = "0123456789ABCDEF";
if (d <= 0) return "0";
int base = 16; // flexible to change in any base under 16
String hex = "";
while (d > 0) {
int digit = d % base; // rightmost digit
hex = digits.charAt(digit) + hex; // string concatenation
d = d / base;
}
return hex;
}
Disclaimer: I use this algorithm in my coding interview. I hope this solution doesn't get too popular :)
Edit June 17 2016 : I added the base
variable to give the flexibility to change into any base : binary, octal, base of 7 ...
According to the comments, this solution is the most elegant so I removed the implementation of Integer.toHexString()
.
Edit September 4 2015 : I found a more elegant solution http://introcs.cs.princeton.edu/java/31datatype/Hex2Decimal.java.html
Check out the code below for decimal to hexadecimal conversion,
import java.util.Scanner;
public class DecimalToHexadecimal
{
public static void main(String[] args)
{
int temp, decimalNumber;
String hexaDecimal = "";
char hexa[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
Scanner sc = new Scanner(System.in);
System.out.print("Please enter decimal number : ");
decimalNumber = sc.nextInt();
while(decimalNumber > 0)
{
temp = decimalNumber % 16;
hexaDecimal = hexa[temp] + hexaDecimal;
decimalNumber = decimalNumber / 16;
}
System.out.print("The hexadecimal value of " + decimalNumber + " is : " + hexaDecimal);
sc.close();
}
}
You can learn more on different ways to convert decimal to hexadecimal in the following link >> java convert decimal to hexadecimal.