Java Byte Array Create toBytes(short sVal, byte[] bytes, boolean bigEndian)

Here you can find the source of toBytes(short sVal, byte[] bytes, boolean bigEndian)

Description

Converts a short into a byte array.

License

Open Source License

Declaration

public static void toBytes(short sVal, byte[] bytes, boolean bigEndian) 

Method Source Code

//package com.java2s;
/*/*from  w  ww . ja va2s.c om*/
 * Copyright 1999-2004 Carnegie Mellon University.  
 * Portions Copyright 2002-2004 Sun Microsystems, Inc.  
 * Portions Copyright 2002-2004 Mitsubishi Electric Research Laboratories.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 *
 */

public class Main {
    /** Converts a short into a byte array. */
    public static void toBytes(short sVal, byte[] bytes, boolean bigEndian) {
        if (bigEndian) {
            bytes[0] = (byte) (sVal >> 8);
            bytes[1] = (byte) (sVal & 0xff);
        } else {
            bytes[0] = (byte) (sVal & 0xff);
            bytes[1] = (byte) (sVal >> 8);
        }
    }
}

Related

  1. toBytes(long... values)
  2. toBytes(Object objValue)
  3. toBytes(Object value)
  4. toBytes(short s)
  5. toBytes(short s)
  6. toBytes(short value)
  7. toBytes(short value)
  8. toBytes(String hex)
  9. toBytes(String hex)