Java Byte Array to String asString(byte[] bs)

Here you can find the source of asString(byte[] bs)

Description

As string.

License

Open Source License

Parameter

Parameter Description
bs the bs

Return

the string

Declaration

private static String asString(byte[] bs) 

Method Source Code

//package com.java2s;
/*//from ww  w .  ja v  a 2s. com
 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Sly Technologies, Inc.
 *
 * This file is part of jNetPcap.
 *
 * jNetPcap is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of 
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * As string.
     * 
     * @param bs
     *          the bs
     * @return the string
     */
    private static String asString(byte[] bs) {
        StringBuilder buf = new StringBuilder();
        for (byte b : bs) {
            if (buf.length() != 0) {
                buf.append(':');
            }
            if (b >= 0 && b < 16) {
                buf.append('0');
            }
            buf.append(Integer.toHexString((b < 0) ? b + 256 : b).toUpperCase());
        }

        return buf.toString();
    }
}

Related

  1. arr2String(byte[] arr, int start)
  2. ASCIIByteArrayToString(byte[] data)
  3. asString(byte[] a, String separator)
  4. asString(byte[] array)
  5. asString(byte[] buf)
  6. asString(byte[] bytes)
  7. asString(byte[] bytes)
  8. ByteArrayToString(byte[] byteArray)