Java Byte Create toByte(long l, byte[] b, int off, boolean bigEndian)

Here you can find the source of toByte(long l, byte[] b, int off, boolean bigEndian)

Description

to Byte

License

Open Source License

Declaration

public static void toByte(long l, byte[] b, int off, boolean bigEndian) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static void toByte(long l, byte[] b, int off, boolean bigEndian) {
        if (bigEndian) {
            b[off + 7] = (byte) (l & 0xff);
            b[off + 6] = (byte) ((l >> 8) & 0xff);
            b[off + 5] = (byte) ((l >> 16) & 0xff);
            b[off + 4] = (byte) ((l >> 24) & 0xff);
            b[off + 3] = (byte) ((l >> 32) & 0xff);
            b[off + 2] = (byte) ((l >> 40) & 0xff);
            b[off + 1] = (byte) ((l >> 48) & 0xff);
            b[off] = (byte) ((l >> 56) & 0xff);
        } else {/*  ww  w  .j a v a  2  s  . com*/
            b[off] = (byte) (l & 0xff);
            b[off + 1] = (byte) ((l >> 8) & 0xff);
            b[off + 2] = (byte) ((l >> 16) & 0xff);
            b[off + 3] = (byte) ((l >> 24) & 0xff);
            b[off + 4] = (byte) ((l >> 32) & 0xff);
            b[off + 5] = (byte) ((l >> 40) & 0xff);
            b[off + 6] = (byte) ((l >> 48) & 0xff);
            b[off + 7] = (byte) ((l >> 56) & 0xff);
        }
    }

    public static void toByte(long[] l, int lOff, int lLen, byte[] b, int bOff, boolean bigEndian) {
        int lEnd = lOff + lLen;
        for (int j = bOff, k = lOff; k < lEnd; j += 8, k++) {
            toByte(l[k], b, j, bigEndian);
        }
    }

    public static void toByte(int i, byte[] b, int off, boolean bigEndian) {
        if (bigEndian) {
            b[off + 3] = (byte) (i & 0xff);
            b[off + 2] = (byte) ((i >> 8) & 0xff);
            b[off + 1] = (byte) ((i >> 16) & 0xff);
            b[off] = (byte) ((i >> 24) & 0xff);
        } else {
            b[off] = (byte) (i & 0xff);
            b[off + 1] = (byte) ((i >> 8) & 0xff);
            b[off + 2] = (byte) ((i >> 16) & 0xff);
            b[off + 3] = (byte) ((i >> 24) & 0xff);
        }
    }

    public static void toByte(int[] i, int iOff, int iLen, byte[] b, int bOff, boolean bigEndian) {
        int iEnd = iOff + iLen;
        for (int j = bOff, k = iOff; k < iEnd; j += 4, k++) {
            toByte(i[k], b, j, bigEndian);
        }
    }

    public static void toByte(short s, byte[] b, int off, boolean bigEndian) {
        if (bigEndian) {
            b[off + 1] = (byte) (s & 0xff);
            b[off] = (byte) ((s >> 8) & 0xff);
        } else {
            b[off] = (byte) (s & 0xff);
            b[off + 1] = (byte) ((s >> 8) & 0xff);
        }
    }

    public static void toByte(short[] s, int sOff, int sLen, byte[] b, int bOff, boolean bigEndian) {
        int sEnd = sOff + sLen;
        for (int j = bOff, k = sOff; k < sEnd; j += 2, k++) {
            toByte(s[k], b, j, bigEndian);
        }
    }

    public static void toByte(float f, byte[] b, int off, boolean bigEndian) {
        toByte(Float.floatToIntBits(f), b, off, bigEndian);
    }

    public static void toByte(float[] f, int fOff, int fLen, byte[] b, int bOff, boolean bigEndian) {
        int fEnd = fOff + fLen;
        for (int j = bOff, k = fOff; k < fEnd; j += 4, k++) {
            toByte(f[k], b, j, bigEndian);
        }
    }

    public static void toByte(double d, byte[] b, int off, boolean bigEndian) {
        toByte(Double.doubleToLongBits(d), b, off, bigEndian);
    }

    public static void toByte(double[] d, int dOff, int dLen, byte[] b, int bOff, boolean bigEndian) {
        int dEnd = dOff + dLen;
        for (int j = bOff, k = dOff; k < dEnd; j += 8, k++) {
            toByte(d[k], b, j, bigEndian);
        }
    }
}

Related

  1. toByte(float[][] in, byte[] out, float min, float max)
  2. toByte(int i)
  3. toByte(int n)
  4. toByte(int rgb)
  5. toByte(long l)
  6. toByte(Number num)
  7. toByte(Object o)
  8. toByte(Object ob, Byte defaultByte)
  9. toByte(Object obj)