Skip to content

Instantly share code, notes, and snippets.

@chrishuan9
Created April 17, 2012 20:47
Show Gist options
  • Save chrishuan9/2408925 to your computer and use it in GitHub Desktop.
Save chrishuan9/2408925 to your computer and use it in GitHub Desktop.
Convert String to Hex and Hex to String
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);
}
}
}
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);
}
}
@ChampIsMe
Copy link

The following code is valid.

public static String str2Hex(String bin) {
        char[] digital = "0123456789ABCDEF".toCharArray();
        StringBuffer sb = new StringBuffer("");
        byte[] bs = bin.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(digital[bit]);
            bit = bs[i] & 0x0f;
            sb.append(digital[bit]);
        }
        return sb.toString();
    }

public static String hexToStr(String hex) {
        String digital = "0123456789ABCDEF";
        char[] hex2char = hex.toCharArray();
        byte[] bytes = new byte[hex.length() / 2];
        int temp;
        for (int i = 0; i < bytes.length; i++) {
            temp = digital.indexOf(hex2char[2 * i]) * 16;
            temp += digital.indexOf(hex2char[2 * i + 1]);
            bytes[i] = (byte) (temp & 0xff);
        }
        return new String(bytes);
    }

Worked like a charm.

@Pablohorch
Copy link

I LOVE THIS POST! THANK !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment