Java Byte Array Create toBytes(long number)

Here you can find the source of toBytes(long number)

Description

to Bytes

License

Apache License

Declaration

public static byte[] toBytes(long number) 

Method Source Code

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

public class Main {
    public static byte[] toBytes(long number) {
        long temp = number;
        byte[] b = new byte[8];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Long(temp & 0xff).byteValue();
            temp = temp >> 8;/*  ww  w . j a v  a2 s  . c o m*/
        }
        return b;
    }

    public static byte[] toBytes(int number) {
        int temp = number;
        byte[] b = new byte[4];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();
            temp = temp >> 8;
        }
        return b;
    }

    public static byte[] toBytes(short number) {
        int temp = number;
        byte[] b = new byte[2];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();
            temp = temp >> 8;
        }
        return b;
    }

    public static byte[] toBytes(float f) {
        int fbit = Float.floatToIntBits(f);
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (fbit >> (24 - i * 8));
        }
        int len = b.length;
        byte[] dest = new byte[len];
        System.arraycopy(b, 0, dest, 0, len);
        byte temp;
        for (int i = 0; i < len / 2; ++i) {
            temp = dest[i];
            dest[i] = dest[len - i - 1];
            dest[len - i - 1] = temp;
        }
        return dest;
    }

    public static byte[] toBytes(String str) {
        return str.getBytes();
    }
}

Related

  1. toBytes(long input)
  2. toBytes(long l)
  3. toBytes(long l, byte[] bytes, int offset, int limit)
  4. toBytes(long lp)
  5. toBytes(long n)
  6. toBytes(long size)
  7. toBytes(long v, byte[] writeBuffer, int o)
  8. toBytes(long val)
  9. toBytes(long value)