encode byte array to Hex String - Java java.lang

Java examples for java.lang:String Hex

Description

encode byte array to Hex String

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(encodeHex(bytes));
    }/*w w w .j  a  v  a2 s. c o m*/

    public static final String encodeHex(byte[] bytes) {
        StringBuffer buf = new StringBuffer(bytes.length * 2);
        int i;

        for (i = 0; i < bytes.length; i++) {
            if (((int) bytes[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString((int) bytes[i] & 0xff, 16));
        }
        return buf.toString().toUpperCase();
    }
}

Related Tutorials