Android Byte Array to String Convert toString(byte[] b)

Here you can find the source of toString(byte[] b)

Description

Convert binary data to a hex-encoded String

Parameter

Parameter Description
b An array containing binary data

Return

A String containing the encoded data

Declaration

public static String toString(byte[] b) 

Method Source Code

//package com.java2s;
import java.io.*;

public class Main {
    private static final String Base16 = "0123456789ABCDEF";

    /**/*from   ww w .  j  a v a  2  s  .c  o m*/
     * Convert binary data to a hex-encoded String
     * @param b An array containing binary data
     * @return A String containing the encoded data
     */
    public static String toString(byte[] b) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        for (int i = 0; i < b.length; i++) {
            short value = (short) (b[i] & 0xFF);
            byte high = (byte) (value >> 4);
            byte low = (byte) (value & 0xF);
            os.write(Base16.charAt(high));
            os.write(Base16.charAt(low));
        }
        return new String(os.toByteArray());
    }
}

Related

  1. toAsciiString(byte[] raw)
  2. toString(byte[] array)
  3. toString(byte[] array, String separator, int frequency)
  4. toString(byte[] theByteArray)
  5. fromBytes(byte[] buf)
  6. fromBytes(byte[] buf, int off, int len)
  7. bytes2String(byte[] value)