Java Array Shorten toShortArray(long value, int length)

Here you can find the source of toShortArray(long value, int length)

Description

Converts a long value to a short array.
The long value could be positive or negative.

License

Open Source License

Parameter

Parameter Description
value The long value
length The length of the array

Return

An array in Low-To-High-Order

Declaration

public static byte[] toShortArray(long value, int length) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from  ww  w.  j a v a2  s  .  co m*/
     * Converts a long value to a short array.<br>
     * The long value could be positive or negative. The result will be set into
     * the array from lowest to highest, truncating bytes if the length of the
     * array is too small. The array will be sorted in Low-To-High-Order!
     *
     * e.g.:<br>
     * 530 dec is 0212 hex, so with length 2 it will be [0]=0x12,[1]=0x02<br>
     * 530 dec is 0212 hex, so with length 3 it will be
     * [0]=0x12,[1]=0x02,[2]=0x00<br>
     *
     * @param value
     *            The long value
     * @param length
     *            The length of the array
     * @return An array in Low-To-High-Order
     */
    public static byte[] toShortArray(long value, int length) {
        byte[] ret = new byte[length];
        toArray(value, ret, 0, length);
        return ret;
    }

    /**
     * Converts a float value to a short array.<br>
     * The float value could be positive or negative. The result will be set
     * into the array from lowest to highest, truncating bytes if the length of
     * the array is too small. The array will be sorted in Low-To-High-Order!
     *
     * e.g.:<br>
     * 530 dec is 0212 hex, so with length 2 it will be [0]=0x12,[1]=0x02<br>
     * 530 dec is 0212 hex, so with length 3 it will be
     * [0]=0x12,[1]=0x02,[2]=0x00<br>
     *
     * @param value
     *            The float value
     * @param length
     *            The length of the array
     * @return An array in Low-To-High-Order
     */
    public static byte[] toShortArray(float value, int length) {
        return toShortArray(convertFloatToHex(value), length);
    }

    /**
     * Converts a long value to a short array.<br>
     * The long value could be positive or negative. The result will be set into
     * the array from lowest to highest, truncating bytes if the length of the
     * array is too small. The array will be sorted in Low-To-High-Order!
     *
     * e.g.:<br>
     * 530 dec is 0212 hex, so with length 2 it will be [0]=0x12,[1]=0x02<br>
     * 530 dec is 0212 hex, so with length 3 it will be
     * [0]=0x12,[1]=0x02,[2]=0x00<br>
     *
     * @param value
     *            The long value
     * @param dest
     *            The destination array
     * @param offset
     *            The offset inside the array
     * @param length
     *            The length of the array
     */
    public static void toArray(long value, byte[] dest, int offset, int length) {
        for (int x = 0; x < length; x++) {
            dest[offset + x] = (byte) (value >>> (8 * x));
        }
    }

    /**
     * Converts a float value to a short array.<br>
     * The float value could be positive or negative. The result will be set
     * into the array from lowest to highest, truncating bytes if the length of
     * the array is too small. The array will be sorted in Low-To-High-Order!
     *
     * e.g.:<br>
     * 530 dec is 0212 hex, so with length 2 it will be [0]=0x12,[1]=0x02<br>
     * 530 dec is 0212 hex, so with length 3 it will be
     * [0]=0x12,[1]=0x02,[2]=0x00<br>
     *
     * @param value
     *            The float value
     * @param dest
     *            The destination array
     * @param offset
     *            The offset inside the array
     * @param length
     *            The length of the array
     */
    public static void toArray(float value, byte[] dest, int offset, int length) {
        toArray(convertFloatToHex(value), dest, offset, length);
    }

    /**
     * Convert a float value to a hex value given as a long.<br>
     *
     * @param floatValue
     *            The float value
     * @return The hex value
     */
    public static long convertFloatToHex(float floatValue) {
        return Float.floatToIntBits(floatValue) & 0xFFFFFFFFL;
    }
}

Related

  1. toShortArray(byte[] data)
  2. toShortArray(double[][] array)
  3. toShortArray(final byte[] array)
  4. toShortArray(Number[] array)
  5. toShortArray(Object[] array)
  6. toShortArray(String str, String separator)
  7. toShortArray(String[] anArray)