byte to Hex String - Java java.lang

Java examples for java.lang:Hex

Description

byte to Hex String

Demo Code


public class Main {


  public static String byte2Hex(byte buf[]) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < buf.length; i++) {
      String hex = Integer.toHexString(buf[i] & 0xFF);
      if (hex.length() == 1) {
        hex = '0' + hex;
      }/*  w  ww . ja va  2  s  .  c om*/
      sb.append(hex);
    }
    return sb.toString();
  }
}

Related Tutorials