Home > database >  Convert byte array to represent a boolean table in java
Convert byte array to represent a boolean table in java

Time:09-28

I am working in active directory where the login hours are stored as a boolean table.

For reference This the value that is stored in active directory and i converted these values to byte array Byte array op -> -1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-10-1

CodePudding user response:

BigInteger(byte[] val) says

Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger. The input array is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element. The val array is assumed to be unchanged for the duration of the constructor call.

So you need to traverse the bits starting with the most significant bit (167).

public static void displayHours(byte[] hours) {
    final String[] DAYS = DateFormatSymbols.getInstance().getWeekdays();    
    BigInteger bi = new BigInteger(hours);
    
    for (int day = 0, bit = hours.length * 8 - 1; day < 7; day  ) {
        System.out.printf("Hours allowed for %s%n", DAYS[day   1]);
        for (int hour = 0; hour < 24; hour  , bit--) {
            boolean allowed = bi.testBit(bit);
            System.out.printf("\tLogin permitted for hour %d?: %b%n", hour, allowed);
        }
    }
}

CodePudding user response:

public void main(String[] args) {
    byte[] arr = {12,2,3,4,5,6,12,3,43,43,34,34,5,5,34,1,32,5,12,13,4}; //size 21
    for(int j = 0; j < arr.length; j = 3) {
        String currentDayBitArray = getBitArrayFromByte(arr[j])   getBitArrayFromByte(arr[j   1])   getBitArrayFromByte(arr[j 2]);
        for(int i = 0; i <=23; i  ){
            if(currentDayBitArray.charAt(i) == '1') {
                System.out.println("hour : "   i   " had permission allowed");
            }
            else {
                System.out.println("hour : "   i   " had permission denied");
            }
        }
      System.out.println("Current day bit array is : "   currentDayBitArray);
    }

    System.out.println();
}

private String getBitArrayFromByte(byte b) {
    return String.format("%8s", Integer.toBinaryString((b   256) % 256))
          .replace(' ', '0');
}

You can do something like that, created a method getBitArrayFromByte to convert from a byte to a bit array as a string (it will replace unnecesary bits representation with 0's so you can have a bit array of length 8)

Build that array for each 3 bytes (meaning one day) and check the bits values

  • Related