Android Byte Array to Hex Convert convertToHexString(byte[] input)

Here you can find the source of convertToHexString(byte[] input)

Description

Converts a byte array into a hexadecimal string.

License

Apache License

Parameter

Parameter Description
input The byte array to convert

Return

The hexadecimal version of the byte array

Declaration

public static String convertToHexString(byte[] input) 

Method Source Code

//package com.java2s;
/*//from   w w  w.j a v a  2s .c  o m
 * Copyright 2013 Kevin Quan (kevin.quan@gmail.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.
 */

import java.util.Locale;

public class Main {
    /**
     * Converts a byte array into a hexadecimal string.  The string will always be lowercase.
     * @param input The byte array to convert
     * @return The hexadecimal version of the byte array
     */
    public static String convertToHexString(byte[] input) {
        if (input == null || input.length == 0) {
            return new String();
        }
        // From http://stackoverflow.com/a/13006907/1339200
        StringBuilder sb = new StringBuilder();
        for (byte b : input) {
            sb.append(String.format(Locale.ENGLISH, "%02x", b & 0xff));
        }
        return sb.toString().toLowerCase(Locale.ENGLISH);
    }
}

Related

  1. toHexString(byte[] bytes, int numBytes)
  2. toHexString(byte[] bytes, int offset, int length)
  3. toHexString(byte[] data)
  4. toHexString(byte[] data, int offset, int length)
  5. toHexStringArray(byte[] data, int offset, int length)
  6. dumpHex(byte[] bytes)
  7. getBinaryStrFromByteArr(byte[] bArr)
  8. toHex(byte[] buf)
  9. toHex(byte[] buf)