Convert hex String To String - Java java.lang

Java examples for java.lang:String Hex

Description

Convert hex String To String

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String txtInHex = "dead";
        System.out.println(hexToString(txtInHex));
    }/* ww w. java 2  s .c om*/

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

Related Tutorials