Java Formatter Usage byteToHexWithPrefix(final byte[] data)

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

Description

Convert bytes to hex + 0x in front.

License

Apache License

Parameter

Parameter Description
data to convert

Return

hex string

Declaration

public static String byteToHexWithPrefix(final byte[] data) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Formatter;

public class Main {
    /**/*from w  ww.  java 2s.  c om*/
     * Convert bytes to hex + 0x in front.
     *
     * @param data
     *            to convert
     * @return hex string
     */
    public static String byteToHexWithPrefix(final byte[] data) {
        return "0x" + byteToHex(data);
    }

    /**
     * Convert bytes to hex without (no 0x in front).
     *
     * @param data
     *            to convert
     * @return hex strng
     */
    public static String byteToHex(final byte[] data) {
        Formatter formatter = new Formatter();
        for (byte b : data) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
}

Related

  1. byteArrayToHex(final byte[] hash)
  2. byteArrayToHexString(byte[] bytes)
  3. byteArrayToHexString(byte[] bytes, int pos, int len)
  4. bytesToHexString(byte[] bytes)
  5. byteToHex(final byte[] bytes)
  6. chatting(String fmt, Object... args)
  7. convertRGBDecToHex(String decimal)
  8. convertTimeToString(double d, String timeFormat)
  9. convertToMillions(double tmp)