Java Hex Calculate toHex(final byte[] bytes)

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

Description

Converts the byte array into a HEX string.

License

Apache License

Parameter

Parameter Description
bytes The bytes to convert.

Return

The string version.

Declaration

public static String toHex(final byte[] bytes) 

Method Source Code

//package com.java2s;
/*//from   w  w w  .  j  av a  2  s  . c o  m
 * #%L
 * IOUtils.java - mongodb-async-driver - Allanbank Consulting, Inc.
 * %%
 * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
 * %%
 * 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.
 * #L%
 */

public class Main {
    /** Hex encoding characters. */
    private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

    /**
     * Converts the byte array into a HEX string.
     *
     * @param bytes
     *            The bytes to convert.
     * @return The string version.
     */
    public static String toHex(final byte[] bytes) {
        final StringBuilder builder = new StringBuilder(bytes.length * 2);
        for (final byte b : bytes) {
            builder.append(HEX_CHARS[(b >> 4) & 0xF]);
            builder.append(HEX_CHARS[b & 0xF]);
        }
        return builder.toString();
    }
}

Related

  1. toHex(final byte b)
  2. toHex(final byte b)
  3. toHex(final byte... bin)
  4. toHex(final byte[] ba)
  5. toHex(final byte[] ba)
  6. toHex(final byte[] bytes)
  7. toHex(final byte[] bytes)
  8. toHex(final byte[] bytes)
  9. toHex(final byte[] bytes)