Java Byte Array Create toBytes(short s)

Here you can find the source of toBytes(short s)

Description

to Bytes

License

Apache License

Declaration

public static byte[] toBytes(short s) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static byte[] toBytes(short s) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (s & 0xff);
        bytes[1] = (byte) ((s >> 8) & 0xff);
        return bytes;
    }//from w  w  w.  j a  v a 2  s .  c  o m

    public static byte[] toBytes(int i) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (i & 0xff);
        bytes[1] = (byte) ((i & 0xff00) >> 8);
        bytes[2] = (byte) ((i & 0xff0000) >> 16);
        bytes[3] = (byte) ((i & 0xff000000) >> 24);
        return bytes;
    }

    public static byte[] toBytes(long l) {
        byte[] bytes = new byte[8];
        bytes[0] = (byte) (l & 0xff);
        bytes[1] = (byte) ((l >> 8) & 0xff);
        bytes[2] = (byte) ((l >> 16) & 0xff);
        bytes[3] = (byte) ((l >> 24) & 0xff);
        bytes[4] = (byte) ((l >> 32) & 0xff);
        bytes[5] = (byte) ((l >> 40) & 0xff);
        bytes[6] = (byte) ((l >> 48) & 0xff);
        bytes[7] = (byte) ((l >> 56) & 0xff);
        return bytes;
    }

    public static byte[] toBytes(double d) {
        return toBytes(Double.doubleToLongBits(d));
    }

    public static byte[] toBytes(float f) {
        return toBytes(Float.floatToIntBits(f));
    }
}

Related

  1. toBytes(long value, int numBytes)
  2. toBytes(long... values)
  3. toBytes(Object objValue)
  4. toBytes(Object value)
  5. toBytes(short s)
  6. toBytes(short sVal, byte[] bytes, boolean bigEndian)
  7. toBytes(short value)
  8. toBytes(short value)
  9. toBytes(String hex)