Android Big Endian Convert shortAsBigEnd(short val)

Here you can find the source of shortAsBigEnd(short val)

Description

Convert a short value into an array in big-endian format.

Declaration

public static byte[] shortAsBigEnd(short val) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  w w.j a v a  2s  .c  o  m
     * Convert a short value into an array in big-endian format.
     * \param val The value to be converted.
     * \returns An array with the value \a val in big-endian format. This
     * array will have 2 bytes long.
     **/
    public static byte[] shortAsBigEnd(short val) {
        byte[] array = new byte[2];
        return shortAsBigEnd(array, 0, val);
    }

    /**
     * Convert a short value into an array in big-endian format, copying the
     * result in the passed array at the point especified.
     * \param a The array where the converted value should be copied.
     * \param i The starting point, int the array \a a where the copy should
     * start.
     * \param v The value to be converted and copied.
     * \returns \a a.
     **/
    public static byte[] shortAsBigEnd(byte[] a, int i, short v) {
        a[i + 0] = (byte) (0xFF & (v >> 8));
        a[i + 1] = (byte) (0xFF & v);
        return a;
    }
}

Related

  1. intAsBigEnd(byte[] a, int i, int v)
  2. intAsBigEnd(int val)
  3. longAsBigEnd(byte[] a, int i, long v)
  4. longAsBigEnd(long val)
  5. shortAsBigEnd(byte[] a, int i, short v)
  6. toBigEndianByteArray(int val, byte[] b, int pos)
  7. toBigEndianByteArray(long val, byte[] b, int pos)
  8. toBigEndianByteArray(short val, byte[] b, int pos)
  9. toBigEndianBytes(int x)