Converting String to Hex in Java
In this example, we will show you the simple program about, how to convert a string to hex in Java. The example program has been tested and shared in the same post.
Example Program (Converter.java)
package com.dineshkrish; import org.apache.commons.codec.binary.Hex; /** * * @author Dinesh Krishnan * */ public class Converter { public static String convert(String str) { char[] ch = Hex.encodeHex(str.getBytes()); return new String(ch); } public static void main(String[] args) { String input = "Dinesh Krishnan"; String output = convert(input); System.out.println("Converted ---> " + output); } }
Maven Dependencies (pom.xml)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dineshkrish</groupId> <artifactId>Converter</artifactId> <version>1.0</version> <dependencies> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> </dependencies> </project>
Outpuut
Converted ---> 44696e657368204b726973686e616e
More from my site
Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.