Java Hex Calculate toHexString(byte[] raw)

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

Description

Convert bytes to (non "0x" prefixed) hexadecimal String

License

Apache License

Declaration

public static String toHexString(byte[] raw) 

Method Source Code

//package com.java2s;
/*/*ww w.  j a va 2s  . c  o  m*/
 * Copyright 2012-2014 Netherlands eScience 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 the following location:
 *
 *     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.
 * 
 * For the full license, see: LICENSE.txt (located in the root folder of this distribution).
 * ---
 */

public class Main {
    public static final char[] HEX_CHAR_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
            'D', 'E', 'F' };

    /** Convert bytes to (non "0x" prefixed) hexadecimal String */
    public static String toHexString(byte[] raw) {
        return toHexString(raw, true);
    }

    /** Convert bytes to (non "0x" prefixed) hexadecimal String */
    public static String toHexString(byte[] raw, boolean upperCase) {
        if (raw == null)
            return null;
        int len = raw.length;
        char chars[] = new char[len * 2];

        for (int i = 0; i < len; i++) {
            chars[i * 2] = HEX_CHAR_TABLE[(raw[i] >> 4) & 0x0f]; // upper byte
            chars[i * 2 + 1] = HEX_CHAR_TABLE[raw[i] & 0x0f]; // lower byte
        }
        String str = new String(chars);
        if (upperCase == false)
            str = str.toLowerCase();
        return str;
    }

    /**
     * Format integer value to Hexadecimal string with optional prefix and a
     * number of digits >= numberOfDigits. A zero is prefix for each digits
     * missing.
     */
    public static String toHexString(String prefix, int value, boolean upperCase, int numberOfDigits) {
        StringBuilder sb = new StringBuilder();
        String hexStr = Integer.toHexString(value);
        if (prefix != null) {
            sb.append(prefix);
        }

        if (hexStr.length() < numberOfDigits) {
            for (int i = 0; i < (numberOfDigits - hexStr.length()); i++) {
                sb.append("0");
            }
        }
        sb.append(hexStr);
        return sb.toString();
    }

    /** Null Pointer Safe toString() method */
    public static String toString(Object obj) {
        if (obj == null)
            return "<NULL>";
        else
            return obj.toString();
    }
}

Related

  1. toHexString(byte[] input)
  2. toHexString(byte[] md)
  3. toHexString(byte[] messagePayload)
  4. toHexString(byte[] paramArrayOfByte)
  5. toHexString(byte[] paramArrayOfByte, String paramString, boolean paramBoolean)
  6. toHexString(byte[] raw)
  7. toHexString(byte[] src)
  8. toHexString(byte[] v)
  9. toHexString(byte[] v)