byte array to String - Java java.lang

Java examples for java.lang:byte Array to String

Description

byte array to String

Demo Code


//package com.java2s;

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

    public static String toString(byte[] theByteArray) {
        StringBuffer out = new StringBuffer();
        for (int i = 0; i < theByteArray.length; i++) {
            String s = Integer.toHexString(theByteArray[i] & 0xff);
            if (s.length() < 2) {
                out.append('0');
            }
            out.append(s).append(' ');
        }
        return out.toString();
    }
}

Related Tutorials