One possible solution:

import java.lang.StringBuilder;

class Test {
  private static final int sizeOfIntInHalfBytes = 8;
  private static final int numberOfBitsInAHalfByte = 4;
  private static final int halfByte = 0x0F;
  private static final char[] hexDigits = { 
    '0', '1', '2', '3', '4', '5', '6', '7', 
    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  };

  public static String decToHex(int dec) {
    StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
    hexBuilder.setLength(sizeOfIntInHalfBytes);
    for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i)
    {
      int j = dec & halfByte;
      hexBuilder.setCharAt(i, hexDigits[j]);
      dec >>= numberOfBitsInAHalfByte;
    }
    return hexBuilder.toString(); 
  }

  public static void main(String[] args) {
     int dec = 305445566;
     String hex = decToHex(dec);
     System.out.println(hex);       
  }
}

Output:

1234BABE

Anyway, there is a library method for this:

String hex = Integer.toHexString(dec);
Answer from kol on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-for-decimal-to-hexadecimal-conversion
Java Program For Decimal to Hexadecimal Conversion - GeeksforGeeks
July 23, 2025 - class GFG { static void decimalToHexadecimal(int number) { if (number == 0) { System.out.print("0"); return; } char[] hexDigits = "0123456789ABCDEF".toCharArray(); char[] result = new char[32]; int i = 0; while (number > 0) { int remainder = number % 16; result[i++] = hexDigits[remainder]; number /= 16; } for (int j = i - 1; j >= 0; j--) { System.out.print(result[j]); } } public static void main(String[] args) { int number = 2545; decimalToHexadecimal(number); } }
Discussions

integer - how to convert decimal to hexadecimal in java - Stack Overflow
I need to convert the rest of the division by 16 to Hexadecimal. I'm using the Integer.toHexString method and pass the value (variable "resto") that I need to convert to hexadecimal, but the ouput ... More on stackoverflow.com
🌐 stackoverflow.com
Convert Decimal to Hexadecimal & other conversions - Oracle Forums
Hello everyone, I've just started with Java recently, and i'm just trying to code some in Java. I came up with this idea for an app to convert decimal -> hexadecimal, decimal -> binary and vice... More on forums.oracle.com
🌐 forums.oracle.com
December 4, 2005
How to Convert Decimal String to Hexadecimal String without ever Converting to Integer
Well, you are still converting it to integer at some point if you are using division, but once you've converted it to a binary string it would be easy to go from binary to hex, as every 4 bits is 1 hex digit. If you are coverting the input string to int you just keep using % 16 and / 16 until it's 0 More on reddit.com
🌐 r/javahelp
5
0
February 16, 2017
Hexadecimal to Decimal conversion using recursive method
Let's walk through all of it. I hate the variable names in this method. I'm going to rewrite it like a person who has some sense: static string ToHexadecimal(int input) { if (input <= 0) { return String.Empty; } var head = ToHexadecimal(input / 16); var remainder = input % 16; var tail = (char)(remainder + (remainder >= 10 ? 'A' - 10 : '0')); return head + tail; } Rewriting some helper methods would make it more clear later, but let's do it line by line, then we'll focus on the tough parts. static string ToHexadecimal(int input) I chose the names because this is a function that takes an int and converts it to a string in hexadecimal format. DECtoHEX is a poor name because an int isn't in any format whatsoever. It's a number. Generally conversion methods are expected to read like "parameter to something". I also like naming my inputs input. That's more clear than DEC1, which implied there's a DEC2 somewhere, or perhaps "December first". It's hard to get anything done when we don't use names that make sense. Now the method is "integer ToHexadecimal string", not "DEC1 DECtoHEX string". Nice. if (input <= 0) { return String.Empty; } Recursive methods have to have a "base" case, the one where they don't recurse. This method will return an empty string if the input is less than or equal to 0, so when thinking about the loop, we'll think about when this is true. var head = ToHexadecimal(input / 16); Here is our loop. It's going to keep the result of calling itself on some other number in a variable named head. I'm guessing from the name this part is intended to be the front of a string. This makes sense. Usually recursive string algorithms involve methods that return one character at a time. And if you think about it, this call will eventually hit the base case. It's integer division, so it rounds. If we call ToHexadecimal(17), it will get here and call ToHexadecimal(17 / 16). Since we're doing integer division, 17 / 16 == 1. That will end up getting to this line again and call ToHexadecimal(1 / 16), which is 0. We saw above that returns "". But we don't know what the other calls return until we keep going. var remainder = input % 16; The % operator is "mod", and finds the remainder of division. So 17 % 16 == 1, 18 % 16 = 2, etc. We don't see how it's used yet, but at least it's straightforward. var tail = (char)(remainder + (remainder >= 10 ? 'A' - 10 : '0')); Don't write lines like this ever. It does way too much on one line. In order to explain this to you I want to take it in chunks. First: do you know what the ternary operator does? IT's the ? : part in the line above. The best way to explain it is to say that this line: int result = IfSomething() ? 100 : 900; Is the same as this code: bool result; if (IsSomething) { result = 100; } else { result = 900; } It's a shortcut for that, but can be used to cram too many things onto one line often. Which is what's going on here. At the far right, we see: (remainder >= 10 ? 'A' - 10 : '0') We could rewrite this: char result; if (remainder >= 10) { result = 'A' - 10; } else { result = '0'; } That might still be a little confusing to you. To understand it, you need to know some assumptions it makes and some dirty details of the char type. To try and keep it short, everything in a computer is a number. A string like "dog" is really 3 numbers. The rules that dictate how numbers correspond to symbols are called "encodings". Long story short: the digits 0 through 9 happen to be right next to the letter 'A' in the encodings we tend to use. Because of that, in a lot of languages the type char is synonymous with numbers, and lots of weird tricks work. So C# here is letting you do something that looks goofy until you know all of that. What's going on here gets even weirder. So let's just go ahead and say what this does and know we'll need it later: "It returns a char. If the remainder is >= 10, that char is whatever is 10 before the letter 'A'. If not, that char is 0. That makes the next bit a little easier: (char)(remainder + ) Aha. Now that we see the whole thing, it starts to make sense. We know that the above is either going to give us '0' or 'A' - 10. So let's walk through some results here. If our remainder is 0-9, we're going to get that number. So let's say remainder is 7. We'll get '0' out of the last part, and our line becomes: (char)(7 + '0') It just so happens the digits are laid out in ASCII next to each other in most encodings. So if you add 7 to the number that means '0', you get '7'. This is a way to turn an int from 0-9 into the char that represents it! Now let's say our remainder is 12. We'll have A - 10 and end up with: (char)(12 + ('A' - 10)) We can rearrange this and see it could actually mean: (char)('A' + 2) And there we go. The letter 2 spaces after 'A' is C, which happens to be how we represent hexadecimal 12. So this line is a silly tricky way for "figure out which character should represent the remainder". Here's how we'd write it if we wanted other people to have half a chance of understanding us: private char GetCharFor(int remainder) { var offset = (remainder >= 10) ? 'A' - 10 : '0'; return (char)(remainder + offset); } That changes the last two lines to: var tail = GetCharFor(remainder); return head + tail; Ah, now it makes more sense. Walk through the calls on paper: ToHexadecimal(135) = "87" head = ToHexadecimal(8) == "8" head = ToHexadecimal(0) == "" remainder = 8 % 16 = 8 return "" + '8' remainder = 135 % 16 = 7 tail = '7' return "8" + '7' See how it works now? More on reddit.com
🌐 r/csharp
8
12
December 21, 2019
🌐
BeginnersBook
beginnersbook.com › 2014 › 07 › java-program-to-convert-decimal-to-hexadecimal
Java Program for Decimal to Hexadecimal Conversion
November 2, 2022 - class JavaExample { public static void main(String args[]) { //give int value int num = 10335; // toHexString() converts int to hex String str = Integer.toHexString(num); System.out.println("Decimal to Hexadecimal: "+str); } }
🌐
How to do in Java
howtodoinjava.com › home › java basics › java – convert decimal to hexadecimal and vice-versa
Java - Convert Decimal to Hexadecimal and Vice-versa
July 28, 2023 - Let’s walk through step by step to Convert 269 (a decimal number) to hexadecimal ... Divide the quotient obtained from the above step by 16 and store the remainder. Repeat the third step until the dividend becomes zero. The final hexadecimal number will be the reverse of the stored remainder. In Java, all Number data types (int, long, float, double) always represent the values in decimal format.
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-decimal-to-hexadecimal-in-java
How to convert Decimal to Hexadecimal in Java
To convert decimal to hexadecimal, use any of the two methods i.e. Let us see an example now to convert decimal to hexadecimal using Integer.toHexString() method. Let us see an example now to convert decimal to hexadecimal using Integer.parseInt()
🌐
w3resource
w3resource.com › java-exercises › basic › java-basic-exercise-20.php
Java - Convert a decimal number to hexadecimal number
February 2, 2026 - It takes a decimal number ('dec_num') as input from the user using the "Scanner" class. It initializes an empty string 'hexdec_num' to store the hexadecimal representation and defines an array 'hex' containing hexadecimal digits (0-9 and A-F).
🌐
Javatpoint
javatpoint.com › java-decimal-to-hex
Java Convert Decimal to Hex - javatpoint
January 31, 2014 - Java Program to swap two string variables without using third or temp variable.
Find elsewhere
🌐
Java Mex
javamex.com › tutorials › conversion › decimal_hexadecimal.shtml
How do you convert to/from hexadecimal in Java?
The extra 'digits' are the letters A-F, which represent decimal values 10-15, as shown in the table below. In Java code (as in many programming languages), hexadecimal nubmers are written by placing 0x before them.
🌐
Pearson
liveexample.pearsoncmg.com › html › Dec2Hex.html
Introduction to Java Programming and Data Structures, 13E, Y. Daniel Liang - Dec2Hex.java
import java.util.Scanner; public ... integer System.out.print("Enter a decimal number: "); int decimal = input.nextInt(); // Convert decimal to hex String hex = ""; while (decimal != 0) { int hexValue = decimal % 16; // Convert a decimal value to a hex digit char hexDigit = (0 ...
🌐
Baeldung
baeldung.com › home › java › java numbers › convert integer to hexadecimal in java
Convert Integer to Hexadecimal in Java | Baeldung
January 8, 2024 - In this tutorial, we’ll learn how to convert an integer value to hexadecimal in Java. We’ll discuss the different methods available using code examples. Before jumping into some code examples, let’s learn how integer to hexadecimal conversion works. Integer numbers use a decimal base(10), ...
🌐
GitHub
github.com › hyperxpro › HexDecimal
GitHub - hyperxpro/HexDecimal: Convert Hex To Decimal and Decimal to Hex In Java · GitHub
Convert Hex To Decimal and Decimal to Hex In Java. Contribute to hyperxpro/HexDecimal development by creating an account on GitHub.
Author: hyperxpro
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › program-decimal-hexadecimal-conversion
Program for decimal to hexadecimal conversion - GeeksforGeeks
// Java program to convert a decimal // number to hexadecimal number import java.io.*; class GFG { // function to convert decimal to hexadecimal static void decToHexa(int n) { // char array to store hexadecimal number char[] hexaDeciNum = new ...
Published: July 30, 2024
🌐
Tpoint Tech
tpointtech.com › java-decimal-to-hex
Java Convert Decimal to Hex - Tpoint Tech
We can convert decimal to hexadecimal in java using Integer.toHexString() method or custom logic. Java Decimal to Hex conversion: Integer.toHexString() The I...
🌐
Edd Mann
eddmann.com › posts › decimal-to-hexadecimal-using-bitwise-bit-shift-operations-in-java
Decimal to Hexadecimal using Bitwise, Bit Shift Operations in Java - Edd Mann
December 18, 2013 - We can then convert this decimal representation into hexadecimal using our built implementation. There are two important points to note about the code below. Firstly, the first two characters of the string returned are removed. This is because hex triplets only use six positions, which means the most significant two are not required. Additionally, Java’s nextInt method is inclusive of 0 and exclusive of the provided maximum value, so we must add one to the desired range to obtain the correct result.
🌐
Oracle
forums.oracle.com › ords › apexds › post › convert-decimal-to-hexadecimal-other-conversions-4064
Convert Decimal to Hexadecimal & other conversions - Oracle Forums
December 4, 2005 - Hello everyone, I've just started with Java recently, and i'm just trying to code some in Java. I came up with this idea for an app to convert decimal -> hexadecimal, decimal -> binary and vice...
🌐
GitHub
gist.github.com › Nishit3479 › f5b96c578caad721df0f27109e4d1fad
Decimal to Hexadecimal using Java · GitHub
Decimal to Hexadecimal using Java · Raw · DtoH.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Binary Hex Decimal Converter
binaryhexconverter.com › decimal-to-hex-converter
Decimal to Hexadecimal Converter
@JAVA N AR - I think you may be incorrect when stating that 13 in hex = 49 in decimal. You have it in reverse. 31 in hex is actually 49 in decimal.
🌐
Physics Forums
physicsforums.com › more sciences and computing › programming and computer science
Representing long decimals in hex in Java • Physics Forums
June 15, 2015 - (I mention Java as that's my programming skill set.) ... Try Planet Source Code. I haven't used it in years but when I used to go there I could almost always find something that helped. ... If you don't mind handling the decimal point yourself you can use the BigInteger class [1] which has a constructor that accept a long string of numbers (without decimal point) and a toString method that takes a radix (use 16 for hexadecimal).