Java Char Create toChar(byte[] si, boolean isReverseOrder)

Here you can find the source of toChar(byte[] si, boolean isReverseOrder)

Description

Convert a byte[] to the corresponding char value.

License

LGPL

Parameter

Parameter Description
si the input array
isReverseOrder True if llittle-endian. False if big-endian (Most significant byte first)

Return

The value coresponding to the byte array

Declaration

public final static char toChar(byte[] si, boolean isReverseOrder) 

Method Source Code

//package com.java2s;
/*//from www .  jav a2  s. c  om
 * Copyright 2005 MBARI
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 2.1 
 * (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.gnu.org/copyleft/lesser.html
 *
 * 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[] (most significant byte first) to the corresponding <b>char</b> value.
     *
     * @param  si  the input array
     * @return     The value coresponding to the byte array
     */
    public final static char toChar(byte[] si) {
        return toChar(si, false);
    }

    /**
     * Convert a byte[] to the corresponding <b>char</b> value.
     *
     * @param  si              the input array
     * @param  isReverseOrder  True if llittle-endian. False if big-endian (Most significant byte first)
     * @return                 The value coresponding to the byte array
     */
    public final static char toChar(byte[] si, boolean isReverseOrder) {
        int i = 0;
        if (isReverseOrder) {
            si = reverseOrder(si, 2);
        }

        char c = (char) ((i | (char) si[0]) << 8);
        c |= (char) si[1];
        return c;
    }

    /**
     * Reverse the ordering of a byte array
     *
     * @param  si
     * @param  i
     * @return
     */
    private final static byte[] reverseOrder(byte[] si, int i) {
        byte[] is = new byte[i];
        for (byte b = 0; b <= i - 1; b++) {
            is[b] = si[i - 1 - b];
        }

        return is;
    }
}

Related

  1. toChar(byte[] b, int off)
  2. toChar(byte[] buffer)
  3. toChar(byte[] buffer)
  4. toChar(byte[] byteArray)
  5. toChar(byte[] bytes)
  6. toChar(final String bitString)
  7. toChar(int c)
  8. toChar(int i)
  9. toChar(int i)