Android Long to Byte Array Convert toByteArray(long val, byte[] b, int pos)

Here you can find the source of toByteArray(long val, byte[] b, int pos)

Description

to Byte Array

Declaration

public static void toByteArray(long val, byte[] b, int pos) 

Method Source Code

//package com.java2s;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

public class Main {

    public static void toByteArray(float val, byte[] b, int pos) {
        try {/*  www. ja v  a 2s .  co m*/
            ByteBuffer bb = ByteBuffer.allocate(4);
            FloatBuffer fb = bb.asFloatBuffer();
            fb.put(val);

            for (int i = 0; i < 4; i++) {
                b[pos + i] = bb.get(i);
            }
            // b = bb.array();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void toByteArray(long val, byte[] b, int pos) {
        assert (pos + 8 <= b.length);
        for (int i = 0; i < 8; ++i) {
            b[pos + i] = (byte) (val & 0x00000000000000FFl);
            val = val >> 8;
        }
    }

    public static void toByteArray(long val, int w, byte[] b, int pos) {
        assert (pos + 8 <= b.length);
        for (int i = 0; i < w; ++i) {
            b[pos + i] = (byte) (val & 0x00000000000000FFl);
            val = val >> 8;
        }
    }

    public static void toByteArray(int val, byte[] b, int pos) {
        assert (pos + 4 <= b.length);
        for (int i = 0; i < 4; ++i) {
            b[pos + i] = (byte) (val & 0x000000FF);
            val = val >> 8;
        }
    }

    public static void toByteArray(short val, byte[] b, int pos) {
        assert (pos + 2 <= b.length);
        b[pos] = (byte) (val & 0x00FF);
        b[pos + 1] = (byte) ((val & 0xFF00) >> 8);
    }
}

Related

  1. longToBytes(long x)
  2. longs2bytesBE(long[] val)
  3. longs2bytesLE(long[] val)
  4. toBytes(long value)
  5. toBytes(long value, byte[] dest, int destPos)
  6. toByteArray(long val, int w, byte[] b, int pos)
  7. getByteArrayByLong(long value, int n)
  8. getByteArrayByLong(long value, int n, boolean isLH)
  9. getBytes(long data)