Java Hex Calculate toHexString(byte b)

Here you can find the source of toHexString(byte b)

Description

Returns the string representation of a byte.

License

Open Source License

Parameter

Parameter Description
b the byte

Return

the string representation of a byte.

Declaration

public static String toHexString(byte b) 

Method Source Code

//package com.java2s;
/*//  w w  w  .  ja  v a2  s.c o  m
* Copyright (c) 2000 - 2005 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  
*
*/

public class Main {
    public final static String NULL = "null";
    /** A table of hex digits */
    private static final char[] hexDigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F' };

    /**
     * Returns the string representation of a byte.
     * @param b the byte
     * @return the string representation of a byte.
     */
    public static String toHexString(byte b) {
        return toHexString(null, b).toString();
    }

    /**
     * Returns the string representation of a byte array.
     * @param b the byte array
     * @return the string representation of a byte array.
     */
    public static String toHexString(byte[] b) {
        return toString(b, null, " ", null);
    }

    /**
     * Appends the string representation of a byte to the string buffer.
     * @param sb the StringBuffer
     * @param b the byte
     * @return the string buffer
     */
    public static StringBuffer toHexString(StringBuffer sb, byte b) {
        if (sb == null)
            sb = new StringBuffer(2);
        sb.append(toHex(b >> 4)); // upper nibble
        sb.append(toHex(b)); // lower nibble
        return sb;
    }

    /**
     * Returns the string representation of a byte array.
     * @param bytes the byte array
     * @return the string representation of a byte array.
     */
    public static String toString(byte[] bytes) {
        return toString(bytes, "[", ",", "]");
    }

    /**
     * Returns the string representation of a byte array.
     * @param bytes the byte array
     * @param beg string to begin the string with
     * @param sep string to separate bytes
     * @param end string to terminate the string with
     * @return the string representation of a byte array.
     */
    public static String toString(byte[] bytes, String beg, String sep, String end) {
        return toString(null, bytes, beg, sep, end).toString();
    }

    /**
     * Returns the string representation of a byte array.
     * @param sb the StringBuffer
     * @param bytes the byte array
     * @param begin string to begin the string with
     * @param sep string to separate bytes from each other
     * @param end string to terminate the string with
     * @return the string representation of a byte array.
     */
    public static StringBuffer toString(StringBuffer sb, byte[] bytes, String begin, String sep, String end) {
        if (bytes != null) {
            if (sb == null) {
                // allocate string buffer of exact size to avoid reallocation
                int len = bytes.length;
                int n1 = ((begin == null) ? 0 : begin.length());
                int n2 = ((sep == null) ? 0 : sep.length());
                int n3 = ((end == null) ? 0 : end.length());
                sb = new StringBuffer(2 * len + n2 * (len - 1) + n1 + n3);
            }
            if (begin != null)
                sb.append(begin);
            for (int i = 0; i < bytes.length; i++) {
                if (i > 0 && sep != null)
                    sb.append(sep);
                toHexString(sb, bytes[i]);
            }
            if (end != null)
                sb.append(end);
        } else {
            if (sb == null) {
                sb = new StringBuffer(NULL);
            } else {
                sb.append(NULL);
            }
        }
        return sb;
    }

    /**
     * Converts array of objects into a comma separated string enclosed
     * into curly brackets.
     */
    public static String toString(Object[] array) {
        return toString(null, array).toString();
    }

    /**
     * Converts array of objects into a comma separated string enclosed
     * into curly brackets and appends it to a StringBuffer.
     *
     * @param sb the destination buffer
     * @param array array of objects
     */
    public static StringBuffer toString(StringBuffer sb, Object[] array) {
        return toString(sb, array, "{", ",", "}");
    }

    /**
     * Converts array of objects into a string enclosed into curly brackets.
     *
     * @param array array of objects
     * @param sep string to separate bytes from each other
     */
    public static String toString(Object[] array, String sep) {
        return toString(array, "{", sep, "}");
    }

    /**
     * Converts array of objects into a comma separated string enclosed
     * into curly brackets.
     *
     * @param array array of objects
     * @param begin string to begin the string with
     * @param end string to terminate the string with
     */
    public static String toString(Object[] array, String begin, String end) {
        return toString(array, begin, ",", end);
    }

    /**
     * Converts array of objects into a string.
     *
     * @param array array of objects
     * @param begin string to begin the string with
     * @param sep string to separate bytes from each other
     * @param end string to terminate the string with
     */
    public static String toString(Object[] array, String begin, String sep, String end) {
        return toString(null, array, begin, sep, end).toString();
    }

    /**
     * Converts array of objects into a string and appends it to
     * a StringBuffer
     *
     * @param array array of objects
     * @param begin string to begin the string with
     * @param sep string to separate bytes from each other
     * @param end string to terminate the string with
     */
    public static StringBuffer toString(StringBuffer sb, Object[] array, String begin, String sep, String end) {
        if (array != null) {
            if (sb == null)
                sb = new StringBuffer(array.length * 5 + 2); // guess
            if (begin != null)
                sb.append(begin);
            for (int i = 0; i < array.length; i++) {
                if (i > 0 && sep != null)
                    sb.append(sep);
                if (array[i] == null) {
                    sb.append(NULL);
                } else {
                    sb.append(array[i].toString());
                }
            }
            if (end != null)
                sb.append(end);
        } else {
            if (sb == null) {
                sb = new StringBuffer(NULL);
            } else {
                sb.append(NULL);
            }
        }
        return sb;
    }

    /**
     * Converts a nibble to a hex character
     * @param   nibble   the nibble to convert.
     * @return the hex character
     */
    private static char toHex(int nibble) {
        return hexDigit[(nibble & 0xF)];
    }
}

Related

  1. toHexString(byte b)
  2. toHexString(byte b)
  3. toHexString(byte b)
  4. toHexString(byte b)
  5. toHexString(byte b)
  6. toHexString(byte b)
  7. toHexString(byte b)
  8. toHexString(byte b)
  9. toHexString(byte b[])