Java Hex Calculate toHexString(byte[] v)

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

Description

Convert a byte array to a hex string

License

Apache License

Parameter

Parameter Description
v a parameter

Declaration

public static String toHexString(byte[] v) 

Method Source Code

//package com.java2s;
/*/* w w w  . j a  v  a2s . c o m*/
 * Copyright (c) 2003-2012 Fred Hutchinson Cancer Research Center
 *
 * 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.
 */

public class Main {
    /**
     * Convert a byte array to a hex string
     * @param v
     * @return
     */
    public static String toHexString(byte[] v) {
        StringBuffer sb = new StringBuffer();
        byte n1, n2;

        for (int c = 0; c < v.length; c++) {
            n1 = (byte) ((v[c] < 0 ? -v[c] + 127 : v[c]) / 0x10);
            n2 = (byte) ((v[c] < 0 ? -v[c] + 127 : v[c]) % 0x10);

            sb.append(n1 >= 0xA ? (char) (n1 - 0xA + 'a') : (char) (n1 + '0'));
            sb.append(n2 >= 0xA ? (char) (n2 - 0xA + 'a') : (char) (n2 + '0'));
        }

        return sb.toString();
    }
}

Related

  1. toHexString(byte[] paramArrayOfByte, String paramString, boolean paramBoolean)
  2. toHexString(byte[] raw)
  3. toHexString(byte[] raw)
  4. toHexString(byte[] src)
  5. toHexString(byte[] v)
  6. toHexString(byte[] val)
  7. toHexString(byte[] value)
  8. toHexString(byte[] value)
  9. toHexString(byte[] value, int startOffset, int maxLength, boolean uppercase, char separator)