Java Byte Array Create toBytes(short value)

Here you can find the source of toBytes(short value)

Description

Convert a short to a byte array

License

Open Source License

Parameter

Parameter Description
value a parameter

Return

the byte array corresponding to the short value

Declaration

public static byte[] toBytes(short value) 

Method Source Code

//package com.java2s;
/*/*w  w w .j  av  a  2 s.co m*/
 * This file is part of org.kalmeo.util.
 * 
 * org.kalmeo.util is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * org.kalmeo.util is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with org.kalmeo.util.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 17 janv. 08
 * Copyright (c) Kalmeo 2007-2008. All rights reserved.
 * http://www.kalmeo.org
 */

public class Main {
    /**
     * Convert a short to a byte array
     * 
     * @param value
     * @return the byte array corresponding to the short value
     */
    public static byte[] toBytes(short value) {
        byte[] bytes = new byte[2];
        toBytes(value, bytes, 0);
        return bytes;
    }

    /**
     * Convert an int to a byte array
     * 
     * @param value
     * @return the byte array corresponding to the int value
     */
    public static byte[] toBytes(int value) {
        byte[] bytes = new byte[4];
        toBytes(value, bytes, 0);
        return bytes;
    }

    /**
     * Convert a long to a byte array
     * 
     * @param value
     * @return the byte array corresponding to the int value
     */
    public static byte[] toBytes(long value) {
        byte[] bytes = new byte[8];
        toBytes(value, bytes, 0);
        return bytes;
    }

    /**
     * Convert a short to a byte array and set it into <code>buffer</code>
     * at specified <code>offset</code>
     * 
     * @param value
     * @param buffer
     * @param offset
     * @return the byte array corresponding to the short value
     */
    public static void toBytes(short value, byte[] buffer, int offset) {
        if (buffer.length - offset < 2) {
            throw new ArrayIndexOutOfBoundsException();
        }

        buffer[offset] = (byte) (value >> 8);
        buffer[offset + 1] = (byte) value;
    }

    /**
     * Convert an int to a byte array and set it into <code>buffer</code>
     * at specified <code>offset</code>
     * 
     * @param value
     * @param buffer
     * @param offset
     * @return the byte array corresponding to the short value
     */
    public static void toBytes(int value, byte[] buffer, int offset) {
        if (buffer.length - offset < 4) {
            throw new ArrayIndexOutOfBoundsException();
        }
        for (int i = 0; i < 4; ++i) {
            buffer[offset + i] = (byte) (value >> ((3 - i) * 8));
        }
    }

    /**
     * Convert a long to a byte array and set it into <code>buffer</code>
     * at specified <code>offset</code>
     * 
     * @param value
     * @param buffer
     * @param offset
     * @return the byte array corresponding to the short value
     */
    public static void toBytes(long value, byte[] buffer, int offset) {
        if (buffer.length - offset < 8) {
            throw new ArrayIndexOutOfBoundsException();
        }
        for (int i = 0; i < 8; ++i) {
            buffer[offset + i] = (byte) (value >> ((7 - i) * 8));
        }
    }
}

Related

  1. toBytes(Object objValue)
  2. toBytes(Object value)
  3. toBytes(short s)
  4. toBytes(short s)
  5. toBytes(short sVal, byte[] bytes, boolean bigEndian)
  6. toBytes(short value)
  7. toBytes(String hex)
  8. toBytes(String hex)
  9. toBytes(String hex)