Convert String to Hex String - Java java.lang

Java examples for java.lang:Hex

Description

Convert String to Hex String

Demo Code


import java.math.BigDecimal;
import java.security.MessageDigest;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        String str = "java2s.com";
        System.out.println(toHexString(str));
    }//from   ww w.  java  2s.  c  o  m
    
    public static String toHexString(String str) {
        byte[] byStr = str.getBytes();
        return parseArr(byStr);
    }
    private static String parseArr(byte[] byStr) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < byStr.length; i++) {
            sb.append("%" + Integer.toHexString(byStr[i]));
        }
        return sb.toString();
    }
}

Related Tutorials