Java Byte Array Encode encode(byte[] buf)

Here you can find the source of encode(byte[] buf)

Description

Converts specifed byte-array to hex-string.

License

Apache License

Declaration

public static String encode(byte[] buf) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.util.Iterator;
import java.util.List;

public class Main {
    /**//from   w w  w .j  a  va  2s .c  o m
     * Converts specifed byte-array to hex-string.
     */
    public static String encode(byte[] buf) {
        StringBuffer result = new StringBuffer(2 * buf.length);

        for (int i = 0; i < buf.length; i++) {
            int byteAsInt = buf[i] & 0xFF;
            if (byteAsInt < 16) {
                result.append("0");
            }
            result.append(Integer.toString(byteAsInt, 16).toLowerCase());
        }

        return result.toString();
    }

    /**
     * Returns specified files as a string list. Returns never null.
     */
    public static String toString(List<File> files, boolean shortName) {
        if (files == null) {
            return "[null]";
        }

        StringBuilder result = new StringBuilder(256);
        for (Iterator<File> iterator = files.iterator(); iterator.hasNext();) {
            File file = iterator.next();
            if (result.length() > 0) {
                result.append(", ");
            }

            String filename = shortName ? file.getName() : file.getAbsolutePath();
            result.append(filename);
        }

        return result.toString();
    }
}

Related

  1. encode(byte[] b)
  2. encode(byte[] buff)
  3. encode(byte[] buffer, int offset, int len, Writer out)
  4. encode(byte[] bytes)
  5. encode(byte[] bytes, boolean withNewLines)