Here is example code for convert String to Hex and in reverse from Hex to String :

import java.io.UnsupportedEncodingException;

/**
 *
 * @author EtaYuy88
 */
public class HexStringConverter
{
    private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
    private static HexStringConverter hexStringConverter = null;

    private HexStringConverter()
    {}

    public static HexStringConverter getHexStringConverterInstance()
    {
        if (hexStringConverter==null) hexStringConverter = new HexStringConverter();
        return hexStringConverter;
    }

    public String stringToHex(String input) throws UnsupportedEncodingException
    {
        if (input == null) throw new NullPointerException();
        return asHex(input.getBytes());
    }

    public String hexToString(String txtInHex)
    {
        byte [] txtInByte = new byte [txtInHex.length() / 2];
        int j = 0;
        for (int i = 0; i < txtInHex.length(); i += 2)
        {
                txtInByte[j++] = Byte.parseByte(txtInHex.substring(i, i + 2), 16);
        }
        return new String(txtInByte);
    }

    private String asHex(byte[] buf)
    {
        char[] chars = new char[2 * buf.length];
        for (int i = 0; i < buf.length; ++i)
        {
            chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
            chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
        }
        return new String(chars);
    }

}

This is how to call it in main class :

import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author EtaYuy88
 */
public class Main {

    private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

    public static void main(String[] args)
    {
        try
        {
            String helloWorldInHex = HexStringConverter.getHexStringConverterInstance().stringToHex("HELLO WORLD");
            System.out.println("'HELLO WORLD' in HEX : " + helloWorldInHex);
            System.out.println("Reconvert to String : " + HexStringConverter.getHexStringConverterInstance().hexToString(helloWorldInHex));
        }
        catch (UnsupportedEncodingException ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

And this is how it run🙂 :

By this way, our plain String may not easily understand by inexperienced users, so we can use it as simple encrypted message or else. Hope this post help you.😉

For info about my newest post :