Click here to Skip to main content
14,643,812 members
Articles » Platforms, Frameworks & Libraries » .NET Framework » General
Article
Posted 13 Feb 2008

Tagged as

Stats

18.6K views
6 bookmarked

LoginHours from DirectoryEntry as a boolean array

Rate this:
1.73 (4 votes)
Please Sign up or sign in to vote.
1.73 (4 votes)
25 Jun 2008CPOL
How to convert the LoginHours byte array from DirectoryEntry into a boolean array.

Introduction

When extracting the LoginHours member from DirectoryEntry, we get a byte array. This array represents the login hours table for the user. It works like this: Every day equals 3 bytes, therefore the array length is always 21 or null. If LoginHours is null, then just make a new array of bytes, with all value set to 255. Now, just to follow up. 1 byte equals 8 bits, and a bit can be 0 or 1 (login is allowed or not.) So, 3 (bytes per day) x 8 (bits) equals 24 (bits), and 1 bit represents an hour in the day. 7 (days) * 3 (byte) * 8 (bit) = 168 bits (1 week has 168 hours).

The LoginHour Class

public class LoginHours
{
    #region Fields

    private int _OffsetAmount;
    private BitArray _BitContainer;

    #endregion

    #region Properties

    public BitArray BitContainer
    {
        get { return _BitContainer; }
        set { _BitContainer = value; }
    }

    public int OffsetAmount
    {
        get { return _OffsetAmount; }
        set { _OffsetAmount = value; }
    }

    #endregion

    #region Contructors

    public LoginHours(BitArray bitcontainer)
    {
        _BitContainer = bitcontainer;
    }

    public LoginHours(bool[] boolvalues)
    {
        _BitContainer = new BitArray(boolvalues);
    }

    public LoginHours(byte[] bytevalues)
    {
        _BitContainer = new BitArray(bytevalues);
    }

    public LoginHours(int length)
    {
        _BitContainer = new BitArray(length);
    }

    public LoginHours(int[] intvalues)
    {
        _BitContainer = new BitArray(intvalues);
    }

    public LoginHours(BitArray bitcontainer, int Offsetamount)
    {
        _BitContainer = bitcontainer;
        this.Offset(Offsetamount);
    }

    public LoginHours(bool[] boolvalues, int Offsetamount)
    {
        _BitContainer = new BitArray(boolvalues);
        this.Offset(Offsetamount);
    }

    public LoginHours(byte[] bytevalues, int Offsetamount)
    {
        _BitContainer = new BitArray(bytevalues);
        this.Offset(Offsetamount);
    }

    public LoginHours(int length, int Offsetamount)
    {
        _BitContainer = new BitArray(length);
        this.Offset(Offsetamount);
    }

    public LoginHours(int[] intvalues, int Offsetamount)
    {
        _BitContainer = new BitArray(intvalues);
        this.Offset(Offsetamount);
    }

    #endregion

    #region Public Methods

    public void Offset()
    {
        _BitContainer = BitArrayOffset(_BitContainer, _OffsetAmount);
    }
    public void Offset(int amount)
    {
        _OffsetAmount = amount;
        _BitContainer = BitArrayOffset(_BitContainer, _OffsetAmount);
    }

    public void OffsetLeft()
    {
        this.Offset(-1);
    }

    public void OffsetRight()
    {
        this.Offset(1);
    }

    public void OffsetReverse()
    {
        this.Offset((-1 * _OffsetAmount));
    }

    public bool[] ToBooleanArray()
    {
        bool[] rtnboolarr = new bool[_BitContainer.Count];
        for (int b = 0; b < _BitContainer.Count; b++)
            rtnboolarr[b] = _BitContainer[b];
        return rtnboolarr;
    }
    public bool[] ToBooleanArray(int startpos, int lenght)
    {
        bool[] rtnboolarr = new bool[lenght];
        for (int b = startpos; b < (startpos + lenght); b++)
            rtnboolarr[b - startpos] = _BitContainer[b];
        return rtnboolarr;
    }
    public bool[] ToBooleanArray(DayOfWeek dayofweek)
    {
        int startpos = (((int)dayofweek) * 24);
        bool[] rtnboolarr = new bool[24];
        for (int b = startpos; b < (startpos + 24); b++)
            rtnboolarr[b - startpos] = _BitContainer[b];
        return rtnboolarr;
    }

    public byte[] ToByteArray()
    {
        int toplevel = _BitContainer.Count / 8;
        byte[] rtnbytearr = new byte[toplevel];
        bool[] conbin;
        int mult = 0;
        for (int i = 0; i < toplevel; i++)
        {
            conbin = new bool[8];
            for (int b = 0; b < 8; b++)
                conbin[b] = _BitContainer.Get(b + mult);
            rtnbytearr[i] = BinToByte(conbin);
            mult += 8;
        }
        return rtnbytearr;
    }
    public byte[] ToByteArray(DayOfWeek dayofweek)
    {
        int startpos = ((int)dayofweek) * 24;
        byte[] rtnbytearr = new byte[3];
        for (int i = 0; i < 3; i++)
            rtnbytearr[i] = BinToByte(ToBooleanArray((startpos + (i * 8)), 8));
        return rtnbytearr;

    }

    public int[] ToInt32Array()
    {
        int toplevel = _BitContainer.Count / 32;
        int[] rtnbytearr = new int[toplevel];
        bool[] conbin;
        int mult = 0;
        for (int i = 0; i < toplevel; i++)
        {
            conbin = new bool[32];
            for (int b = 0; b < 32; b++)
                conbin[b] = _BitContainer.Get(b + mult);
            rtnbytearr[i] = BinToByte(conbin);
            mult += 32;
        }
        return rtnbytearr;
    }

    #endregion

    #region Internal Methods

    internal BitArray BitArrayOffset(BitArray ArrayToOffsetFrom, int OffsetValue)
    {
        if (OffsetValue == 0)
            return ArrayToOffsetFrom;
        BitArray rtnbitArr = new BitArray(ArrayToOffsetFrom.Count);
        int Offset = 0;
        for (int i = 0; i < ArrayToOffsetFrom.Count; i++)
        {
            Offset = i + OffsetValue;
            if (Offset > ArrayToOffsetFrom.Count - 1)
                Offset = Offset - ArrayToOffsetFrom.Count;
            if (Offset < 0)
                Offset = ArrayToOffsetFrom.Count + Offset;
            rtnbitArr.Set(Offset, ArrayToOffsetFrom[i]);
        }
        return rtnbitArr;
    }

    internal byte BinToByte(bool[] boolvals)
    {
        int multiplier = 1;
        int rtnval = 0;

        for (int i = 0; i < boolvals.Length; i++)
        {
            rtnval = rtnval + (Convert.ToInt16(boolvals[i]) * multiplier);
            multiplier = multiplier * 2;
        }
        return (byte)rtnval;
    }

    #endregion
}

Using the code

The class is very easy to use. Here is a sample:

LoginHours lh = new LoginHours(new byte[] { 128, 255, 255, 255, 255, 
                255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 
                255, 255, 255, 255, 255 });
bool[] LoginHoursBool = lh.ToBooleanArray();
int DaySeperator = 1;
foreach (bool b in LoginHoursBool)
{
    Debug.Write(Convert.ToInt16(b) + " ");
    if (DaySeperator % 24 == 0)
        Debug.Write("\n");
     DaySeperator++;
}

I your server has some time offset due to regional issues, use the method:

BitArrayOffset(offsetvalue);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
GeneralMy vote of 3 Pin
Amir Mohammad Nasrollahi8-Aug-13 20:12
professionalAmir Mohammad Nasrollahi8-Aug-13 20:12 
Generalerror: rtnboolarr = _bitarray Pin
Zitz24-Jun-08 20:07
MemberZitz24-Jun-08 20:07 
GeneralRe: error: rtnboolarr = _bitarray Pin
Paw Jershauge26-Jun-08 3:34
MemberPaw Jershauge26-Jun-08 3:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.