Saturday, August 24, 2013

convert unicode string to hexadecimal in java code

    public static void main(String[] args) {
        try {
            System.out.println(toHex(JOptionPane.showInputDialog("input")));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Notifier.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static String toHex(String arg) throws UnsupportedEncodingException {
        String res = "";
        char[] chars = new char[arg.length()];
        arg.getChars(0, arg.length(), chars, 0);
        for (int i = 0; i < chars.length; i++) {
            res += "|"+ String.format("%4s", Integer.toHexString(chars[i])).replace(' ', '0');;
        }
        return res;
    }

No comments:

Post a Comment