Java Byte Array to Hex byteToHexDisplayString(byte[] b)

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

Description

Converts byte array to a string representation of hex bytes for display purposes.

License

Open Source License

Parameter

Parameter Description
b the source buffer.

Return

"hexized" string representation of bytes.

Declaration

static String byteToHexDisplayString(byte[] b) 

Method Source Code

//package com.java2s;
/*/* w w  w  .  j  a  v a  2 s  .  c  o  m*/
 * Microsoft JDBC Driver for SQL Server
 * 
 * Copyright(c) Microsoft Corporation All rights reserved.
 * 
 * This program is made available under the terms of the MIT License. See the LICENSE file in the project root for more information.
 */

public class Main {
    final static char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };

    /**
     * Converts byte array to a string representation of hex bytes for display purposes.
     * 
     * @param b
     *            the source buffer.
     * @return "hexized" string representation of bytes.
     */
    static String byteToHexDisplayString(byte[] b) {
        if (null == b)
            return "(null)";
        int hexVal;
        StringBuilder sb = new StringBuilder(b.length * 2 + 2);
        sb.append("0x");
        for (byte aB : b) {
            hexVal = aB & 0xFF;
            sb.append(hexChars[(hexVal & 0xF0) >> 4]);
            sb.append(hexChars[(hexVal & 0x0F)]);
        }
        return sb.toString();
    }
}

Related

  1. byteToHex(final byte b)
  2. byteToHex(final byte b)
  3. bytetoHex(final byte data, final StringBuffer buffer)
  4. byteToHex(int val)
  5. byteToHex(int val, StringBuffer sb)
  6. byteToHexWord(byte in)
  7. byteToLowerHex(final byte b)
  8. byteToTwoHexString(final byte data)