Java Char Create toChar(byte[] buffer)

Here you can find the source of toChar(byte[] buffer)

Description

to Char

License

Apache License

Declaration

public static String toChar(byte[] buffer) 

Method Source Code

//package com.java2s;
/*/*w  w  w .  j a  v a  2  s. c  o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 to a character for printing. If the
     * byte value is < 32 -and hence unprintable- the byte is
     * returned as a two digit hex value
     * @param b byte
     * @return the printable character string
     */
    public static String toChar(byte b) {
        if (b >= 0x20) {
            return Character.toString((char) b);
        } else {
            return String.format("%02x", b);
        }
    }

    public static String toChar(byte[] buffer) {
        StringBuilder builder = new StringBuilder(buffer.length);
        for (byte b : buffer) {
            builder.append(toChar(b));
        }
        return builder.toString();
    }
}

Related

  1. toChar(byte b)
  2. toChar(byte value)
  3. toChar(byte[] b, int off)
  4. toChar(byte[] buffer)
  5. toChar(byte[] byteArray)
  6. toChar(byte[] bytes)
  7. toChar(byte[] si, boolean isReverseOrder)
  8. toChar(final String bitString)