Java Byte to ASCII byteToASCII(final byte b)

Here you can find the source of byteToASCII(final byte b)

Description

Retrieves the printable ASCII representation of the provided byte.

License

CDDL license

Parameter

Parameter Description
b The byte for which to retrieve the printable ASCII representation.

Return

The printable ASCII representation of the provided byte, or a space if the provided byte does not have printable ASCII representation.

Declaration

public static char byteToASCII(final byte b) 

Method Source Code

//package com.java2s;
/*/*www. jav a 2 s.  c om*/
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2009-2010 Sun Microsystems, Inc.
 * Portions copyright 2011-2015 ForgeRock AS.
 */

public class Main {
    /**
     * Retrieves the printable ASCII representation of the provided byte.
     *
     * @param b
     *            The byte for which to retrieve the printable ASCII
     *            representation.
     * @return The printable ASCII representation of the provided byte, or a
     *         space if the provided byte does not have printable ASCII
     *         representation.
     */
    public static char byteToASCII(final byte b) {
        if (isPrintable(b)) {
            return (char) b;
        }
        return ' ';
    }

    /**
     * Returns whether the byte is a printable ASCII character.
     *
     * @param b
     *          The byte for which to determine whether it is printable ASCII
     * @return true if the byte is a printable ASCII character
     */
    public static boolean isPrintable(final byte b) {
        return 32 <= b && b <= 126;
    }
}

Related

  1. byteToASCII(final byte b)
  2. byteToAscii(final byte byteToAscii)
  3. byteToAscii(int val)