Here's a short way to convert it to hex:

public String toHex(String arg) {
    return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}
Answer from Kaleb Pederson on Stack Overflow
🌐
Delft Stack
delftstack.com › home › howto › java › string to hex java
How to Convert String to Hex in Java | Delft Stack
February 2, 2024 - This output demonstrates the successful conversion of the input string to its hexadecimal representation using Apache Commons Codec. The BigInteger class in Java provides a way to represent integers of arbitrary precision.
Discussions

How can I convert a string into hex in Java - Stack Overflow
If you want to write the raw bytes of your hex string to a file (not the hex string representing the raw bytes themselves), don't use FileWriter, and don't convert the raw byte array to a String. According to the Javadoc for FileWriter: More on stackoverflow.com
🌐 stackoverflow.com
Java String to hex string
I'm afraid I can't really replicate your issue. Could you paste the stacktrace? Also, you use cromosoma += ((int) (Math.random() * 10)) % 2; Why not use: cromosoma += (int) (Math.random() * 2); ? More on reddit.com
🌐 r/learnjava
3
1
October 23, 2016
How to convert hex string to Java string? - Stack Overflow
For logging purpose we are converting the logs to byte array and then to hex string. I want to get it back in a Java String, but I am not able to do so. The hex string in log file looks something ... More on stackoverflow.com
🌐 stackoverflow.com
java - string to hex value - Stack Overflow
Is there any java utility to convert string to hex value (integer) ? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Mkyong
mkyong.com › home › java › how to convert string to hex in java
How to convert String to Hex in Java - Mkyong.com
February 28, 2020 - Would you have any advice or guidance, tutorial or code to that regard? most of the tips online always go straight to encrypt and decrypt but there is really no much on the individual steps. Any help will be greatly appreciated. Thanks in advace ... I think the method convertStringToHex may need a little modification. It doesn’t handle hex below 0x10. For example, if the argument is “I\tlove Java!”, the second byte will become just “9” instead of “09”. ... StringBuffer hex = new StringBuffer(); int each; for(int i = 0; i < chars.length; i++){ each = (int)chars[i]; if (each < 0x10) // in this case, ASCII code is 1 digit hex { hex.append("0"); } hex.append(Integer.toHexString(each)); }
🌐
Baeldung
baeldung.com › home › java › java string › introduction to hexformat in java
Introduction to HexFormat in Java | Baeldung
January 8, 2024 - However, Java 17 introduces java.util.HexFormat, a utility class that enables the conversion of primitive types, byte arrays, or char arrays to a hex string and vice versa.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-a-string-to-hexadecimal-and-vice-versa-format-in-java
How to Convert a String to Hexadecimal and vice versa format in java?
September 1, 2025 - To convert a string to hexadecimal in Java, we can use the toHexString() method of the Integer class accepts an integer as a parameter and returns a hexadecimal string.
🌐
W3docs
w3docs.com › home › code snippets › java › converting a string to hexadecimal in java
Converting A String To Hexadecimal In Java | W3docs
String input = "Hello World"; StringBuilder hexString = new StringBuilder(); for (char c : input.toCharArray()) { hexString.append(String.format("x", (int) c)); } System.out.println(hexString.toString()); // prints "48656c6c6f20576f726c64" Copy · For Java 19+, consider using the built-in java.util.HexFormat to avoid external dependencies.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › util › HexFormat.html
HexFormat (Java SE 17 & JDK 17)
April 21, 2026 - HexFormat formatFingerprint = HexFormat.ofDelimiter(":").withUpperCase(); byte[] bytes = {0, 1, 2, 3, 124, 125, 126, 127}; String str = formatFingerprint.formatHex(bytes); byte[] parsed = formatFingerprint.parseHex(str); assert(Arrays.equals(bytes, parsed)); // The formatted string is: "00:01:02:03:7C:7D:7E:7F" This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of HexFormat may have unpredictable results and should be avoided. The equals method should be used for comparisons. This class is immutable and thread-safe. Unless otherwise noted, passing a null argument to any method will cause a NullPointerException to be thrown.
🌐
Coderanch
coderanch.com › t › 372764 › java › convert-String-hexadecimal
how to convert String to hexadecimal value (Java in General forum at Coderanch)
i understand the logic of how to do it by hand but not sure how to write the code for it. are there any methods such as in the Integer class that can help? such as String num = 85; Integer b; b = Integer.valueOf(num, 16); i must be doing it wrong because i get NumberFormatExceptions. any help is much appreciated! ... try using Integer.decode(hex).
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › java ascii to hex conversion (and vice-versa)
Java ASCII to Hex Conversion (and vice-versa)
May 25, 2024 - Many times we do need to convert the string values in ASCII from/to Hex format. In this Java tutorial, I am giving you two small code snippets that you can utilize to convert a string from Hex to ASCII or ASCII to Hex, as you want.
🌐
Baeldung
baeldung.com › home › java › java array › converting between byte arrays and hexadecimal strings in java
Converting Between Byte Arrays and Hexadecimal Strings in Java | Baeldung
March 26, 2025 - In addition to the methods discussed earlier for converting between byte arrays and hexadecimal strings, Java 17 introduced a convenient alternative through the java.util.HexFormat class.
🌐
Java Mex
javamex.com › tutorials › conversion › decimal_hexadecimal.shtml
How do you convert to/from hexadecimal in Java?
This article explains hexadecimal numbers and then shows how you can convert a string to an integer in Java and vice versa in order to perform hex/decimal conversions.
🌐
GeeksforGeeks
geeksforgeeks.org › java › hexformat-in-java
HexFormat in Java - GeeksforGeeks
April 8, 2023 - If you use the parseHex() method with that same string, it will convert it back into a byte array. HexFormat is a mechanism that is commonly used in computer programming and helps convert data between different formats.
🌐
Reddit
reddit.com › r/learnjava › java string to hex string
r/learnjava on Reddit: Java String to hex string
October 23, 2016 -

I'm trying to create a genetic algorithm and I've got this code:

public Cromosoma() {
    this.setRepresentacionBinaria(this.generarAleatoriamenteCromosoma());
}

private String generarAleatoriamenteCromosoma() {
    String cromosoma = "";
    for (int i = 1; i <= 40; i++) {
        cromosoma += ((int) (Math.random() * 10)) % 2;
    }
    return cromosoma;
}

public String getRepresentacionBinaria() {
    return this.representacionBinaria;
}

public String getRepresentacionHexadecimal() {

    return Long.toHexString(Long.parseLong(this.getRepresentacionBinaria(), 2));
}

getRepresentacionBinaria() generates a random string of 40 characters(0s and 1s), I need that string to be a Hexadecimal string(without the x), this code does it, the problem comes when Long.parseLong() parses the string and returns a hexadecimal number with leading zeros(which I need) and gives me this error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
🌐
myCompiler
mycompiler.io › view › LBZahDea44l
String to hex (Java) - myCompiler
import java.util.*; import java.lang.*; import java.io.*; // The main method must be in a class named "Main". class Main { public static void main(String[] args) { String stringToConvert = "55134966-F636-11EB-8002-000C2984568D"; convertStringToHex(stringToConvert); } private static void convertStringToHex(String str) { StringBuilder stringBuilder = new StringBuilder(); char[] charArray = str.toCharArray(); for (char c : charArray) { String charToHex = Integer.toHexString(c); stringBuilder.append(charToHex); } System.out.println("Converted Hex from String: "+stringBuilder.toString()); } } Output ·
🌐
LabEx
labex.io › tutorials › java-how-to-use-the-tohexstring-method-to-convert-a-long-value-to-hexadecimal-in-java-414173
How to use the toHexString() method to convert a long value to hexadecimal in Java | LabEx
This tutorial will guide you through the process of converting a long value to its hexadecimal representation using the toHexString() method in Java.