byte to hex via Integer.toHexString - Java java.lang

Java examples for java.lang:Hex

Description

byte to hex via Integer.toHexString

Demo Code


//package com.java2s;

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

    public static String byte2hex(byte[] b) { 
        StringBuffer sb = new StringBuffer(b.length * 2);
        String tmp = "";
        for (int n = 0; n < b.length; n++) {

            tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (tmp.length() == 1) {
                sb.append("0");
            }
            sb.append(tmp);
        }
        return sb.toString().toUpperCase();
    }
}

Related Tutorials