Java Byte Array to Hex String bytesToHexString(byte[] b, int length)

Here you can find the source of bytesToHexString(byte[] b, int length)

Description

Converts byte array to a string representation of hex bytes.

License

Open Source License

Parameter

Parameter Description
b the source buffer.

Return

"hexized" string representation of bytes.

Declaration

static String bytesToHexString(byte[] b, int length) 

Method Source Code

//package com.java2s;
/*//w ww  .ja  v a 2 s  .com
 * 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.
     * 
     * @param b
     *            the source buffer.
     * @return "hexized" string representation of bytes.
     */
    static String bytesToHexString(byte[] b, int length) {
        StringBuilder sb = new StringBuilder(length * 2);
        for (int i = 0; i < length; i++) {
            int hexVal = b[i] & 0xFF;
            sb.append(hexChars[(hexVal & 0xF0) >> 4]);
            sb.append(hexChars[(hexVal & 0x0F)]);
        }
        return sb.toString();
    }
}

Related

  1. bytesToHexString(byte bytes[])
  2. bytesToHexString(byte data[])
  3. bytesToHexString(byte[] _bytes)
  4. bytesToHexString(byte[] array)
  5. bytesToHexString(byte[] b)
  6. bytesToHexString(byte[] bArray)
  7. bytesToHexString(byte[] bArray)
  8. bytesToHexString(byte[] bs)
  9. bytesToHexString(byte[] buf)