Java Char Create toChar(byte[] buffer)

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

Description

to Char

License

Open Source License

Declaration

public static String toChar(byte[] buffer) 

Method Source Code

//package com.java2s;
/**/* ww  w.  ja v a2  s .  c o  m*/
 * (C) Copyright IBM Corp. 2015, 2016
 *
 * 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
 *
 *     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 {
    public static String toChar(byte[] buffer) {
        StringBuilder builder = new StringBuilder(buffer.length);
        for (byte b : buffer) {
            builder.append(toChar(b));
        }
        return builder.toString();
    }

    /**
     * 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);
        }
    }
}

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)