Java Byte Array Convert To bytesToText(byte[] bytes)

Here you can find the source of bytesToText(byte[] bytes)

Description

Convert bytes to text

License

Open Source License

Parameter

Parameter Description
bytes a parameter

Return

text for the input bytes

Declaration

private static String bytesToText(byte[] bytes) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  www .jav a2s  . co m*/
     * Convert bytes to text
     *
     * @param bytes
     * @return text for the input bytes
     */
    private static String bytesToText(byte[] bytes) {
        StringBuffer sb = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            int num = bytes[i];
            if (num < 0)
                num = 127 + (num * -1); // fix negative back to positive
            String hex = Integer.toHexString(num);
            if (hex.length() == 1) {
                hex = "0" + hex; // ensure 2 digits
            }
            sb.append(hex);
        }

        return sb.toString();
    }
}

Related

  1. bytesToStringAscii(byte[] bytes)
  2. bytesToStringMac(byte[] mac)
  3. bytesToStringUTFCustom(byte[] bytes)
  4. bytesToSVar64(final byte[] buffer, final int offset)
  5. bytesToTagBE(byte[] bytes, int off)
  6. BytesToUInt16(byte[] pb)
  7. bytesToVar32(final byte[] buffer, final int offset)