Android Byte Array to Hex Convert toHex(byte[] bytes)

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

Description

Encodes an array of bytes as hex symbols.

License

Apache License

Parameter

Parameter Description
bytes the array of bytes to encode

Return

the resulting hex string

Declaration

public static String toHex(byte[] bytes) 

Method Source Code

//package com.java2s;
/**//from   w w w.ja  v a2s. co  m
 * Copyright 2011 bccapi.com
 *
 * 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 {
    /**
     * Encodes an array of bytes as hex symbols.
     * 
     * @param bytes
     *           the array of bytes to encode
     * @return the resulting hex string
     */
    public static String toHex(byte[] bytes) {
        return toHex(bytes, null);
    }

    /**
     * Encodes an array of bytes as hex symbols.
     * 
     * @param bytes
     *           the array of bytes to encode
     * @param separator
     *           the separator to use between two bytes, can be null
     * @return the resulting hex string
     */
    public static String toHex(byte[] bytes, String separator) {
        return toHex(bytes, 0, bytes.length, separator);
    }

    /**
     * Encodes an array of bytes as hex symbols.
     * 
     * @param bytes
     *           the array of bytes to encode
     * @param offset
     *           the start offset in the array of bytes
     * @param length
     *           the number of bytes to encode
     * @return the resulting hex string
     */
    public static String toHex(byte[] bytes, int offset, int length) {
        return toHex(bytes, offset, length, null);
    }

    /**
     * Encodes a single byte to hex symbols.
     * 
     * @param byte the byte to encode
     * @return the resulting hex string
     */
    public static String toHex(byte b) {
        StringBuilder sb = new StringBuilder();
        appendByteAsHex(sb, b);
        return sb.toString();
    }

    /**
     * Encodes an array of bytes as hex symbols.
     * 
     * @param bytes
     *           the array of bytes to encode
     * @param offset
     *           the start offset in the array of bytes
     * @param length
     *           the number of bytes to encode
     * @param separator
     *           the separator to use between two bytes, can be null
     * @return the resulting hex string
     */
    public static String toHex(byte[] bytes, int offset, int length,
            String separator) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int unsignedByte = bytes[i + offset] & 0xff;

            if (unsignedByte < 16) {
                result.append("0");
            }

            result.append(Integer.toHexString(unsignedByte));
            if (separator != null && i + 1 < length) {
                result.append(separator);
            }
        }
        return result.toString();
    }

    public static void appendByteAsHex(StringBuilder sb, byte b) {
        int unsignedByte = b & 0xFF;
        if (unsignedByte < 16) {
            sb.append("0");
        }
        sb.append(Integer.toHexString(unsignedByte));
    }
}

Related

  1. ConvertHexByteArrayToString( byte[] byteArrayToConvert)
  2. toHex(byte[] array)
  3. toHex(byte[] array, int offset, int len)
  4. toHex(byte[] buf)
  5. toHex(byte[] bytes)
  6. toHex(byte[] bytes, String separator)
  7. toHex(byte[] bytes, int offset, int length)
  8. toHex(byte[] bytes, int offset, int length, String separator)
  9. toHex(byte[] dataBytes)