Java Byte Array to String byteToString(final boolean prettyPrint, int byteValue)

Here you can find the source of byteToString(final boolean prettyPrint, int byteValue)

Description

Returns a byte value as either a 2-char hex string, or if pretty printing, and the byte value is a printable ASCII character, as a quoted ASCII char, unless it is a single quote character itself, in which case it will still be represented as a hex byte.

License

Open Source License

Parameter

Parameter Description
prettyPrint Whether to pretty print the byte value.
value The byte value to represent as a string.

Return

A string containing the byte value as a string.

Declaration

public static String byteToString(final boolean prettyPrint, int byteValue) 

Method Source Code

//package com.java2s;

public class Main {
    private static final int QUOTE_CHARACTER_VALUE = 39;
    private static final int START_PRINTABLE_ASCII = 32;
    private static final int END_PRINTABLE_ASCII = 126;

    /**/*from  ww  w  .  ja va  2  s .  co  m*/
     * Returns a byte value as either a 2-char hex string, or if
     * pretty printing, and the byte value is a printable ASCII
     * character, as a quoted ASCII char, unless it is a single quote
     * character itself, in which case it will still be represented as
     * a hex byte.
     * 
     * @param prettyPrint Whether to pretty print the byte value.
     * @param value The byte value to represent as a string.
     * @return A string containing the byte value as a string.
     */
    public static String byteToString(final boolean prettyPrint, int byteValue) {
        String result = null;
        if (prettyPrint) {
            if (byteValue >= START_PRINTABLE_ASCII && byteValue <= END_PRINTABLE_ASCII
                    && byteValue != QUOTE_CHARACTER_VALUE) {
                result = String.format(" '%c' ", byteValue);
            } else {
                result = String.format(" %02x ", byteValue);
            }
        } else {
            result = String.format("%02x", byteValue);
        }
        return result;
    }
}

Related

  1. byteToString(byte[] bytes)
  2. byteToString(byte[] data)
  3. bytetoString(byte[] digest)
  4. byteToString(byte[] input)
  5. bytetoString(byte[] tb)
  6. byteToString(int[] byteData)