Java Integer to Short intToShortArray(final int intValue)

Here you can find the source of intToShortArray(final int intValue)

Description

Converts an int value to a 4-byte length short array.

License

Apache License

Parameter

Parameter Description
intValue int value.

Return

short array.

Declaration

public static short[] intToShortArray(final int intValue) 

Method Source Code

//package com.java2s;
/*//from ww w.j ava  2  s  . co m
 * Copyright 2013, Carsten J?ger
 *
 * 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 {
    /**
     * Constant 4.
     */
    public static final byte CONST_4 = 4;
    /**
     * Constant 8.
     */
    public static final byte CONST_8 = 8;
    /**
     * Constant 255.
     */
    public static final short CONST_255 = 255;

    /**
     * Converts an int value to a 4-byte length short array.
     *
     * @param intValue int value.
     * @return short array.
     */
    public static short[] intToShortArray(final int intValue) {
        final short[] result = new short[CONST_4];
        for (byte i = CONST_4 - 1; i >= 0; --i) {
            result[i] |= ((intValue >>> CONST_8 * ((CONST_4 - 1) - i)) & (CONST_255));
        }
        return result;
    }
}

Related

  1. convertIntegerToShort(Integer intValue)
  2. intToShort(int i)
  3. intToShort(int[] values)
  4. intToShortBytes(int a)
  5. intToShorts(int n)