Android Working with Byte

package com.nhancv.chart.utils;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
* Created by nhancao on 9/28/16.
*/

public class NByte {

    private static final short INT_BYTE = Integer.SIZE / Byte.SIZE;
    private static final short SHORT_BYTE = Short.SIZE / Byte.SIZE;
    private static final short LONG_BYTE = Long.SIZE / Byte.SIZE;
    private static final short FLOAT_BYTE = Float.SIZE / Byte.SIZE;
    private static final short DOUBLE_BYTE = Double.SIZE / Byte.SIZE;


    /**
     * Convert long to byte array
     *
     * @param x
     * @return
     */
    public static byte[] longToBytes(long x) {
        ByteBuffer buffer = ByteBuffer.allocate(LONG_BYTE);
        buffer.putLong(0, x);
        return buffer.array();
    }

    /**
     * Convert byte array to long
     *
     * @param bytes
     * @return
     */
    public static long bytesToLong(byte[] bytes) {
        return bytesToLong(bytes, 0);
    }

    /**
     * Convert byte array to long with offset
     *
     * @param bytes
     * @param offset
     * @return
     */
    public static long bytesToLong(byte[] bytes, int offset) {
        ByteBuffer buffer = ByteBuffer.allocate(LONG_BYTE);
        buffer.put(bytes, offset, LONG_BYTE);
        buffer.flip();
        return buffer.getLong();
    }

    /**
     * Convert int to byte array
     *
     * @param x
     * @return
     */
    public static byte[] intToBytes(int x) {
        ByteBuffer buffer = ByteBuffer.allocate(INT_BYTE);
        buffer.putInt(0, x);
        return buffer.array();
    }

    /**
     * Convert byte array to int
     *
     * @param bytes
     * @return
     */
    public static int bytesToInt(byte[] bytes) {
        return bytesToInt(bytes, 0);
    }

    /**
     * Convert byte array to int with offset
     *
     * @param bytes
     * @param offset
     * @return
     */
    public static int bytesToInt(byte[] bytes, int offset) {
        ByteBuffer buffer = ByteBuffer.allocate(INT_BYTE);
        buffer.put(bytes, offset, INT_BYTE);
        buffer.flip();
        return buffer.getInt();
    }

    /**
     * Convert short to byte array
     *
     * @param x
     * @return
     */
    public static byte[] shortToBytes(short x) {
        ByteBuffer buffer = ByteBuffer.allocate(SHORT_BYTE);
        buffer.putInt(0, x);
        return buffer.array();
    }


    /**
     * Convert byte array to short
     *
     * @param bytes
     * @return
     */
    public static short bytesToShort(byte[] bytes) {
        return bytesToShort(bytes, 0);
    }

    /**
     * Convert byte array to short with offset
     *
     * @param bytes
     * @param offset
     * @return
     */
    public static short bytesToShort(byte[] bytes, int offset) {
        ByteBuffer buffer = ByteBuffer.allocate(SHORT_BYTE);
        buffer.put(bytes, offset, SHORT_BYTE);
        buffer.flip();
        return buffer.getShort();
    }

    /**
     * Convert float to byte array
     *
     * @param x
     * @return
     */
    public static byte[] floatToBytes(float x) {
        ByteBuffer buffer = ByteBuffer.allocate(FLOAT_BYTE);
        buffer.putFloat(0, x);
        return buffer.array();
    }

    /**
     * Convert byte array to float
     *
     * @param bytes
     * @return
     */
    public static float bytesToFloat(byte[] bytes) {
        return bytesToFloat(bytes, 0);
    }

    /**
     * Convert byte array to float with offset
     *
     * @param bytes
     * @return
     */
    public static float bytesToFloat(byte[] bytes, int offset) {
        ByteBuffer buffer = ByteBuffer.allocate(FLOAT_BYTE);
        buffer.put(bytes, offset, FLOAT_BYTE);
        buffer.flip();
        return buffer.getFloat();
    }

    /**
     * Convert double to byte array
     *
     * @param x
     * @return
     */
    public static byte[] doubleToBytes(double x) {
        ByteBuffer buffer = ByteBuffer.allocate(DOUBLE_BYTE);
        buffer.putDouble(0, x);
        return buffer.array();
    }

    /**
     * Convert byte array to double
     *
     * @param bytes
     * @return
     */
    public static double bytesToDouble(byte[] bytes) {
        return bytesToDouble(bytes, 0);
    }

    /**
     * Convert byte array to double with offset
     *
     * @param bytes
     * @return
     */
    public static double bytesToDouble(byte[] bytes, int offset) {
        ByteBuffer buffer = ByteBuffer.allocate(DOUBLE_BYTE);
        buffer.put(bytes, offset, DOUBLE_BYTE);
        buffer.flip();
        return buffer.getDouble();
    }

    /**
     * Make new byte array after remove first n byte in original byte array. Original array will not change
     *
     * @param bytes
     * @param n
     * @return
     */
    public static byte[] removeFirstNByte(byte[] bytes, int n) {
        return Arrays.copyOfRange(bytes, n, bytes.length);
    }

    /**
     * Make new byte array by cutting original array
     *
     * @param bytes
     * @param startIndex
     * @param endIndex
     * @return
     */
    public static byte[] subArray(byte[] bytes, int startIndex, int endIndex) {
        return Arrays.copyOfRange(bytes, startIndex, endIndex);
    }

}

Leave a Reply

Your email address will not be published.Required fields are marked *