Java Hex Calculate toHexString(final byte[] data)

Here you can find the source of toHexString(final byte[] data)

Description

Converts binary data to string hex representation

License

Apache License

Parameter

Parameter Description
data binary data

Return

binary data hex representation

Declaration

public static String toHexString(final byte[] data) 

Method Source Code

//package com.java2s;
/*//from   w w  w  .j  a  v a2s  . co m
 * Copyright (c) 2008-2011 Ivan Khalopik.
 *
 * 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 HI_BYTE_MASK = 0xf0;
    private static final int LOW_BYTE_MASK = 0x0f;
    private static final char[] HEX_SYMBOLS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
            'd', 'e', 'f', };

    /**
     * Converts binary data to string hex representation
     *
     * @param data binary data
     * @return binary data hex representation
     */
    public static String toHexString(final byte[] data) {
        final StringBuffer builder = new StringBuffer(2 * data.length);
        for (byte item : data) {
            builder.append(HEX_SYMBOLS[(HI_BYTE_MASK & item) >>> 4]);
            builder.append(HEX_SYMBOLS[(LOW_BYTE_MASK & item)]);
        }
        return builder.toString();
    }
}

Related

  1. toHexString(final byte[] bytes)
  2. toHexString(final byte[] bytes)
  3. toHexString(final byte[] bytes)
  4. toHexString(final byte[] bytes)
  5. toHexString(final byte[] bytes)
  6. toHexString(final byte[] data)
  7. toHexString(final byte[] data)
  8. toHexString(final byte[] fieldData)
  9. toHexString(final byte[] raw)