Java Array to String arrayToString(byte[] bytes)

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

Description

array To String

License

Apache License

Declaration

public static String arrayToString(byte[] bytes) 

Method Source Code

//package com.java2s;
/*/* w  w w  .  ja  va 2s.  c  o  m*/
 * Copyright 2013 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    private static final int BYTES_PER_LINE = 16;
    private static final int HALF_BYTES_PER_LINE = BYTES_PER_LINE / 2;

    public static String arrayToString(byte[] bytes) {
        StringBuilder result = new StringBuilder(bytes.length * 4);
        int i = 0;
        while (i < bytes.length) {
            int value = bytes[i] & 0xFF;
            result.append(hexChar(value / 16));
            result.append(hexChar(value % 16));
            i++;
            if (i % BYTES_PER_LINE == 0) {
                result.append('\n');
            } else if (i % HALF_BYTES_PER_LINE == 0) {
                result.append("   ");
            } else {
                result.append(' ');
            }
        }
        return result.toString();
    }

    private static char hexChar(int value) {
        if (value < 0 || value > 15) {
            throw new IllegalArgumentException();
        }
        return (char) (value < 10 ? ('0' + value) : ('a' + (value - 10)));
    }
}

Related

  1. arrayToStr(String[] o)
  2. ArrayToStr(String[] strArr, String seperatedBy)
  3. arrayToStr(String[] strArray)
  4. arrayToString(byte[] arg)
  5. arrayToString(byte[] array)
  6. arrayToString(byte[] source, int index, short size)
  7. arrayToString(double d[])
  8. arrayToString(double... a)
  9. arrayToString(double[] array)