Java Formatter Usage toHexString(final byte[] data)

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

Description

Converts the given byte array to hex string

License

Apache License

Parameter

Parameter Description
data the byte array to be converted to hex string (which cannot be null )

Exception

Parameter Description
NullPointerException if the given byte array is null

Return

the converted hex string

Declaration

public static String toHexString(final byte[] data) throws NullPointerException 

Method Source Code


//package com.java2s;
/*/* w w w .j a  va2  s  .  c  o  m*/
 * #%L
 * JavaCreed Secure Properties Encoder
 * %%
 * Copyright (C) 2012 - 2015 Java Creed
 * %%
 * 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%
 */

import java.util.Formatter;

public class Main {
    /**
     * Converts the given byte array to hex string
     *
     * @param data
     *          the byte array to be converted to hex string (which cannot be {@code null})
     * @return the converted hex string
     * @throws NullPointerException
     *           if the given byte array is {@code null}
     */
    public static String toHexString(final byte[] data) throws NullPointerException {
        try (Formatter formatter = new Formatter()) {
            for (final byte b : data) {
                formatter.format("%02x", b);
            }
            return formatter.toString();
        }
    }
}

Related

  1. millisToHumanTime(long milliseconds)
  2. msToHumanReadableDelta(long start)
  3. numberWithLeadingZeroes(int n, int totalChars)
  4. showVxlanHeaderOutput()
  5. substitution(Formatter formatter, int flags, int width, int precision, StringBuilder obj)
  6. toReadableSize(long bytes)
  7. toTwoDigit(double f)
  8. toUUIDFormat(byte[] bytes)
  9. validateCondition(boolean condition, String messageFormat, Object... messageArgs)