Decimal To Hex In Java

Chapter: Math Class Last Updated: 04-05-2016 18:32:44 UTC

Program:

            /* ............... START ............... */
                
public class JavaDecimalToHex {

	public static void main(String[] args) {
		System.out.println("Decimal 1234: Hex " + dec2hex(1234));
		System.out.println("Decimal 255: Hex " + dec2hex(255));
		System.out.println("Decimal 1024: Hex " + dec2hex(1024));
	}

	static String dec2hex(int decimal) {

		char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
				'9', 'A', 'B', 'C', 'D', 'E', 'F' };

		int x = decimal;
		int r = 0;
		String results = "";

		while (x != 0) {
			r = x % 16;
			x = (int) Math.floor(x / 16);
			results = hex[r] + results;
		}
		return (results);
	}
}
                /* ............... END ............... */
        

Output

Decimal 1234: Hex 4D2
Decimal 255: Hex FF
Decimal 1024: Hex 400

Tags

Decimal To Hex, Java, Math

Similar Programs Chapter Last Updated
Java Program To Calculate Standard Deviation Math Class 21-09-2017
Java Program To Display Factors Of Number Math Class 12-09-2017
Java Program To Find Power Of Number Using pow() Function Math Class 29-08-2017
Java Program To Find Quotient And Remainder Math Class 25-08-2017
Java Matrix Multiplication Math Class 18-05-2017
Java Matrix Subtraction Math Class 18-05-2017
Java Matrix Addition Math Class 17-05-2017
LCM And GCD Of Two Numbers In Java Math Class 14-05-2017
Java Cube Root Of Number Math Class 10-03-2017
Java Program To Calculate Area Of Right Triangle Math Class 16-12-2016
Java Program To Find Rectangle Perimeter Math Class 15-06-2016
Java Program To Find Circle Perimeter Math Class 15-06-2016
Math.Floor In Java Example Math Class 14-06-2016
Math.ceil In Java Example Math Class 14-06-2016
Java Program To Find Square Root Of Given Number Math Class 14-06-2016
Random Number Generation In Java Example Math Class 14-06-2016
Quadratic Equation In Java Example Math Class 14-06-2016
Java Program To Displays Powers of 2 Example Math Class 14-06-2016
Java Program To Check For Scalar Matrix Math Class 30-05-2016
Java Program To Find Saddle Point In Square Matrix Math Class 30-05-2016
Decimal To Octal Converstion In Java Math Class 30-05-2016
Decimal To Binary Converstion In Java Math Class 30-05-2016
Java Program To Check For Upper Triangular Matrix Math Class 28-05-2016
Java Program To Check For Pronic Number Math Class 28-05-2016
Java Program To Find Value Of Mobius Function For number Math Class 28-05-2016
Java Program To Check For Lower Triangular Matrix Math Class 28-05-2016
Java Program To Check For Harshad Number Math Class 28-05-2016
Java Program To Check Fascinating Number Math Class 28-05-2016
Java Program To Check For Evil Number Math Class 28-05-2016
Java Program To Check Bouncy Number Increasing Decreasing Number Math Class 07-02-2018

1 2