Java Byte Array Create toByteArray(short foo)

Here you can find the source of toByteArray(short foo)

Description

Description of the Method

License

Open Source License

Parameter

Parameter Description
foo Description of Parameter

Return

Description of the Returned Value

Declaration

public static byte[] toByteArray(short foo) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*www.java  2 s  .c o  m*/
     * Description of the Method
     * 
     * @param foo
     *            Description of Parameter
     * @return Description of the Returned Value
     */
    public static byte[] toByteArray(short foo) {
        return toByteArray(foo, new byte[2]);
    }

    /**
     * Description of the Method
     * 
     * @param foo
     *            Description of Parameter
     * @return Description of the Returned Value
     */
    public static byte[] toByteArray(int foo) {
        return toByteArray(foo, new byte[4]);
    }

    /**
     * Description of the Method
     * 
     * @param foo
     *            Description of Parameter
     * @return Description of the Returned Value
     */
    public static byte[] toByteArray(long foo) {
        return toByteArray(foo, new byte[8]);
    }

    /**
     * Description of the Method
     * 
     * @param foo
     *            Description of Parameter
     * @param array
     *            Description of Parameter
     * @return Description of the Return Value
     * @eturn Description of the Returned Value
     */
    private static byte[] toByteArray(long foo, byte[] array) {
        int len = array.length;
        for (int iInd = 0; iInd < len; iInd++) {
            array[iInd] = (byte) ((foo >> ((len - iInd - 1) * 8)) & 0x000000FF);
        }
        return array;
    }
}

Related

  1. toByteArray(long value)
  2. toByteArray(long value)
  3. toByteArray(Number[] array)
  4. toByteArray(Object obj)
  5. toByteArray(Object value)
  6. toByteArray(String address)
  7. toByteArray(String binString)
  8. toByteArray(String bytesStr, int byteLength)
  9. toByteArray(String content, String charsetName)