Here you can find the source of bytes2String(byte[] data)
public static String bytes2String(byte[] data)
//package com.java2s; import java.io.UnsupportedEncodingException; public class Main { public static String bytes2String(byte[] data) { String result = null;//from w w w . j a v a 2 s.c o m int availableLen = data.length; for (int i = 0; i < availableLen; i++) { if (data[i] == 0) { availableLen = i; break; } } try { if (availableLen == 1) { result = String.valueOf((int) data[0]); } else { result = new String(data, 0, availableLen, "UTF-8"); } } catch (UnsupportedEncodingException e) { return null; } return result; } }