Java Byte Array Create toByteArray(char c)

Here you can find the source of toByteArray(char c)

Description

Converts an char to the corresponding byte[] array.

License

LGPL

Parameter

Parameter Description
c char input

Return

Byte array corresponding to the input

Declaration

public final static byte[] toByteArray(char c) 

Method Source Code

//package com.java2s;
/*// ww  w  .j a  v  a2  s  .  c  o m
 * 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 {
    /**
     * Converts an <b>char</b> to the corresponding byte[] array. Most significant byte is first.
     *
     * @param  c  char input
     * @return    Byte array corresponding to the input
     */
    public final static byte[] toByteArray(char c) {
        return toByteArray(c, false);
    }

    /**
     * Converts a <b>double</b> to the corresponding byte[] array. Most significant byte is first.
     *
     * @param  d  Integer input
     * @return    Byte array corresponding to the input
     */
    public final static byte[] toByteArray(double d) {
        return toByteArray(d, false);
    }

    /**
     * Converts a <b>float</b> to the corresponding byte[] array. Most significant byte is first.
     *
     * @param  f  float input
     * @return    Byte array corresponding to the input
     */
    public final static byte[] toByteArray(float f) {
        return toByteArray(f, false);
    }

    /**
     * Converts an <b>integer</b> to the corresponding byte[] array. Most significant byte is first.
     *
     * @param  i  Integer input
     * @return    Byte array corresponding to the input
     */
    public final static byte[] toByteArray(int i) {
        return toByteArray(i, false);
    }

    /**
     * Converts a <b>long</b> to the corresponding byte[] array. Most significant byte is first.
     *
     * @param  l  long input
     * @return    Byte array corresponding to the input
     */
    public final static byte[] toByteArray(long l) {
        return toByteArray(l, false);
    }

    /**
     * Converts a <b>short</b> to the corresponding byte[] array. Most significant byte is first.
     *
     * @param  i  Integer input
     * @return    Byte array corresponding to the input
     */
    public final static byte[] toByteArray(short i) {
        return toByteArray(i, false);
    }

    /**
     *  Description of the Method
     *
     * @param  c               Description of the Parameter
     * @param  isReverseOrder  Description of the Parameter
     * @return                 Description of the Return Value
     */
    public final static byte[] toByteArray(char c, boolean isReverseOrder) {
        byte[] si = new byte[2];
        for (byte i = 0; i <= 1; i++) {
            si[i] = (byte) (c >>> (1 - i) * 8);
        }

        if (isReverseOrder) {
            si = reverseOrder(si, 2);
        }

        return si;
    }

    /**
     * Converts a <b>double</b> to the corresponding byte[] array
     *
     * @param  isReverseOrder  True if the ordering is least significant byte to
     * most signifigant byte, false for most significant to least.
     * @param  d               Description of the Parameter
     * @return                 Byte array corresponding to the input
     */
    public final static byte[] toByteArray(double d, boolean isReverseOrder) {
        byte[] si = new byte[8];
        // Double var_double = new Double(d);
        long l = Double.doubleToLongBits(d);
        si = toByteArray(l, isReverseOrder);
        return si;
    }

    /**
     * Converts a <b>float</b> to the corresponding byte[] array
     *
     * @param  f               Short input
     * @param  isReverseOrder  True if the ordering is least significant byte to
     * most signifigant byte, false for most significant to least.
     * @return                 Byte array corresponding to the input
     */
    public final static byte[] toByteArray(float f, boolean isReverseOrder) {
        byte[] si = new byte[4];
        // Float var_float = new Float(f);
        int i = Float.floatToIntBits(f);
        si = toByteArray(i, isReverseOrder);
        return si;
    }

    /**
     * Converts an <b>integer</b> to the corresponding byte[] array
     *
     * @param  i               Integer input
     * @param  isReverseOrder  True if the ordering is least significant byte to
     * most signifigant byte, false for most significant to least.
     * @return                 Byte array corresponding to the input
     */
    public final static byte[] toByteArray(int i, boolean isReverseOrder) {
        byte[] si = new byte[4];
        for (byte b = 0; b <= 3; b++) {
            si[b] = (byte) (i >>> (3 - b) * 8);
        }

        if (isReverseOrder) {
            si = reverseOrder(si, 4);
        }

        return si;
    }

    /**
     * Converts a <b>long</b> to the corresponding byte[] array
     *
     * @param  isReverseOrder  True if the ordering is least significant byte to
     * most signifigant byte, false for most significant to least.
     * @param  l               Description of the Parameter
     * @return                 Byte array corresponding to the input
     */
    public final static byte[] toByteArray(long l, boolean isReverseOrder) {
        byte[] si = new byte[8];
        for (byte i = 0; i <= 7; i++) {
            si[i] = (byte) (int) (l >>> (7 - i) * 8);
        }

        if (isReverseOrder) {
            si = reverseOrder(si, 8);
        }

        return si;
    }

    /**
     * Converts a <b>short</b> to the corresponding byte[] array
     *
     * @param  i               Short input
     * @param  isReverseOrder  True if the ordering is least significant byte to
     * most signifigant byte, false for most significant to least.
     * @return                 Byte array corresponding to the input
     */
    public final static byte[] toByteArray(short i, boolean isReverseOrder) {
        byte[] si = new byte[2];
        for (byte b = 0; b <= 1; b++) {
            si[b] = (byte) (i >>> (1 - b) * 8);
        }

        if (isReverseOrder) {
            si = reverseOrder(si, 2);
        }

        return si;
    }

    /**
     * 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. toByteArray(byte arg, int length)
  2. toByteArray(byte b)
  3. toByteArray(byte byteArray)
  4. toByteArray(byte num)
  5. toByteArray(byte value)
  6. toByteArray(char[] charArray)
  7. toByteArray(char[] chars)
  8. toByteArray(char[] chars)
  9. toByteArray(char[] src)