Android Byte Array to Hex Convert toHexString(byte[] data)

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

Description

Converts a byte array into a String of its hexadecimal representation.

License

BSD License

Parameter

Parameter Description
data The array of bytes to be converted.

Return

The hexadecimal String representation of the byte array.

Declaration

public static String toHexString(byte[] data) 

Method Source Code

//package com.java2s;
/**/*from  w ww .  j  a  va2 s  . c om*/
 * android-user-listeners
 *  ----------------------
 *
 * Copyright 2013 UPM
 * Copyright 2013 Emergya
 *
 * Licensed under the New BSD license. You may not use this file except in
 * compliance with this License.
 *
 * You may obtain a copy of the License at
 * https://github.com/gpii/universal/LICENSE.txt
 *
 * The research leading to these results has received funding from the European
 * Union's Seventh Framework Programme (FP7/2007-2013) under grant agreement 
 * #289016.
 **/

public class Main {
    /**
     * Converts a byte array into a String of its hexadecimal representation.
     * This method should be called statically.
     * @param data The array of bytes to be converted.
     * @return The hexadecimal String representation of the byte array.
     */
    public static String toHexString(byte[] data) {
        int c;
        String res = "", s;
        for (byte aux : data) {
            c = aux & 0xff;
            s = Integer.toHexString(c);
            if (s.length() == 1)
                res += "0";
            res += s;
        }
        return res;
    }
}

Related

  1. bytesAsHexString(byte[] bytes, int maxShowBytes)
  2. toHexString(byte[] bytes)
  3. toHexString(byte[] bytes)
  4. toHexString(byte[] bytes, int numBytes)
  5. toHexString(byte[] bytes, int offset, int length)
  6. toHexString(byte[] data, int offset, int length)
  7. toHexStringArray(byte[] data, int offset, int length)
  8. convertToHexString(byte[] input)
  9. dumpHex(byte[] bytes)