Java Hex Calculate toHexString(byte[] value)

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

Description

to Hex String

License

Apache License

Declaration

public static String toHexString(byte[] value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2015 InfinitiesSoft Solutions Inc.
 *
 * 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//w  w  w.  ja v  a  2 s . com
 *
 *      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 {
    private static final char DEFAULT_HEX_DELIMITER = ':';

    public static String toHexString(byte[] value) {
        return toHexString(value, DEFAULT_HEX_DELIMITER);
    }

    public static String toHexString(byte[] value, char separator) {
        return toString(value, separator, 16);
    }

    public static String toString(byte[] value, char separator, int radix) {
        int digits = (int) (Math.round((float) Math.log(256) / Math.log(radix)));
        StringBuffer buf = new StringBuffer(value.length * (digits + 1));
        for (int i = 0; i < value.length; i++) {
            if (i > 0) {
                buf.append(separator);
            }
            int v = (value[i] & 0xFF);
            String val = Integer.toString(v, radix);
            for (int j = 0; j < digits - val.length(); j++) {
                buf.append('0');
            }
            buf.append(val);
        }
        return buf.toString();
    }
}

Related

  1. toHexString(byte[] src)
  2. toHexString(byte[] v)
  3. toHexString(byte[] v)
  4. toHexString(byte[] val)
  5. toHexString(byte[] value)
  6. toHexString(byte[] value, int startOffset, int maxLength, boolean uppercase, char separator)
  7. toHexString(char c)
  8. toHexString(final byte b)
  9. toHexString(final byte b)