Java Byte to Char charAt(byte[] b, int s)

Here you can find the source of charAt(byte[] b, int s)

Description

char At

License

Apache License

Declaration

public static char charAt(byte[] b, int s) 

Method Source Code

//package com.java2s;
/**// ww w  . j  a v a2 s.c om
 * 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 {
    public static char charAt(byte[] b, int s) {
        if (s >= b.length) {
            throw new ArrayIndexOutOfBoundsException("Are you crazy?");
        }
        int c = b[s] & 0xff;
        switch (c >> 4) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
            return (char) c;

        case 12:
        case 13:
            return (char) (((c & 0x1F) << 6) | ((b[s + 1]) & 0x3F));

        case 14:
            return (char) (((c & 0x0F) << 12) | (((b[s + 1]) & 0x3F) << 6) | (((b[s + 2]) & 0x3F) << 0));

        default:
            throw new IllegalArgumentException();
        }
    }
}

Related

  1. byteToChar(byte c)
  2. byteToChar(byte chr)
  3. byteToChar(byte[] bytes)
  4. byteToChar(int b, char unprintable)
  5. ByteToSafeChar(byte _bt)