Android Byte Array Bit Get getBitData(byte[] src, int start, int end)

Here you can find the source of getBitData(byte[] src, int start, int end)

Description

new byte[]{13, 0} => 00001101, 00000000 => 8-11 = new byte[]{1, 0, 1, 1}

License

Open Source License

Parameter

Parameter Description
src a parameter
index a parameter

Declaration

public static byte[] getBitData(byte[] src, int start, int end) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w w  w .ja  va  2  s.  co  m*/
     * new byte[]{13, 0} => 00001101, 00000000 => 8 = 1, 10 = 1, 11 = 1
     * 
     * @param src
     * @param index
     * @return
     */
    public static byte getBitData(byte[] src, int index) {
        return getBitData(src, index, true);
    }

    public static byte getBitData(byte[] src, int index, boolean isLH) {
        return getBitData(src, index, index + 1, isLH)[0];
    }

    /**
     * new byte[]{13, 0} => 00001101, 00000000 => 8-11 = new byte[]{1, 0, 1, 1}
     * 
     * @param src
     * @param index
     * @return
     */
    public static byte[] getBitData(byte[] src, int start, int end) {
        return getBitData(src, start, end, true);
    }

    public static byte[] getBitData(byte[] src, int start, int end,
            boolean isLH) {
        byte[] result = null;
        StringBuffer sb = new StringBuffer();
        for (byte b : src) {
            sb.append(getBinaryString(b));
        }
        String bitString = (isLH ? sb : sb.reverse()).substring(start, end);
        result = new byte[bitString.length()];
        for (int i = 0; i < bitString.length(); i++) {
            result[i] = Byte.valueOf(String.valueOf(bitString.charAt(i)));
        }
        return result;
    }

    /**
     * 7 => 00000111
     * 
     * @param src
     * @return
     */
    public static String getBinaryString(long src) {

        String binaryString = Long.toBinaryString(src);
        String temp = "";
        for (int i = 0; i < Long.SIZE - binaryString.length(); i++) {
            temp += "0";
        }
        binaryString = temp + binaryString;
        return binaryString;
    }

    /**
     * 7 => 00000111
     * 
     * @param src
     * @return
     */
    public static String getBinaryString(byte src) {
        String binaryString = Integer.toBinaryString(src);
        if (binaryString.length() > Byte.SIZE) {
            binaryString = binaryString.substring(binaryString.length()
                    - Byte.SIZE);
        } else {
            String temp = "";
            for (int i = 0; i < Byte.SIZE - binaryString.length(); i++) {
                temp += "0";
            }
            binaryString = temp + binaryString;
        }
        return binaryString;
    }
}

Related

  1. getBitData(byte[] src, int index)
  2. getBitData(byte[] src, int index, boolean isLH)
  3. getBitData(byte[] src, int start, int end, boolean isLH)
  4. getByteNetOrder(byte[] theBytes, int idx, int size)
  5. getByteNetOrderTo_unit16(byte[] theBytes, int idx)
  6. getByteNetOrderTo_unit32(byte[] theBytes, int idx)