c#位运算实现权限操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringByte
{
    class Program
    {
        static void Main(string[] args)
        {
            AuthorityUtil au = new AuthorityUtil();
            User user = new User("Jack",20);

            user.Authority = au.AddAuthority(user.Authority, Authority.logisticsBuy_show); //{ 0x00, 0x00, 0x00, 0x01 };
            user.Authority = au.AddAuthority(user.Authority, Authority.config_update);     //{ 0x00, 0x00, 0x00, 0x40 }
            user.Authority = au.AddAuthority(user.Authority, Authority.config_del);        //{ 0x00, 0x00, 0x00, 0x80 };
            user.Authority = au.AddAuthority(user.Authority, Authority.logisticsSell_show);  //{ 0x00, 0x00, 0x01, 0x00 };
            user.Authority = au.AddAuthority(user.Authority, Authority.logisticsSell_update);//{ 0x00, 0x00, 0x08, 0x00 };


            Console.WriteLine(user.Authority); //预计是"000009c1"

            user.Authority = au.RemoveAuthority(user.Authority,Authority.config_del);

            Console.WriteLine(user.Authority); //预计是"00000941"

            Console.WriteLine(au.CheckAuthority(user.Authority, Authority.logsticsBuy_del)); //预计是false

            Console.WriteLine(au.CheckAuthority(user.Authority, Authority.config_update)); //预计是true

            Console.ReadKey();
        }

    }
}

权限操作工具类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringByte
{
    public class AuthorityUtil
    {
        //Byte数组长度为4,每一长度为8位即8个权限,总权限32

        List<Byte[]> AuthorityList = new List<Byte[]>();
        String PrimaryString = "00000000";      //传入权限字段为空时赋初值

        private Byte[] logisticsBuy_show =      { 0x00, 0x00, 0x00, 0x01 };
        private Byte[] logsticsBuy_add =        { 0x00, 0x00, 0x00, 0x02 };
        private Byte[] logsticsBuy_del =        { 0x00, 0x00, 0x00, 0x04 };
        private Byte[] logsticsBuy_update =     { 0x00, 0x00, 0x00, 0x08 };

        private Byte[] config_show =            { 0x00, 0x00, 0x00, 0x10 };
        private Byte[] config_add =             { 0x00, 0x00, 0x00, 0x20 };
        private Byte[] config_update =          { 0x00, 0x00, 0x00, 0x40 };
        private Byte[] config_del =             { 0x00, 0x00, 0x00, 0x80 };

        private Byte[] logisticsSell_show =     { 0x00, 0x00, 0x01, 0x00 };
        private Byte[] logisticsSell_add =      { 0x00, 0x00, 0x02, 0x00 };
        private Byte[] logisticsSell_del =      { 0x00, 0x00, 0x04, 0x00 };
        private Byte[] logisticsSell_update =   { 0x00, 0x00, 0x08, 0x00 };

        private Byte[] logisticsSell_check =    { 0x00, 0x00, 0x10, 0x00 };
        private Byte[] contract_keepArch =      { 0x00, 0x00, 0x20, 0x00 };
        private Byte[] contract_keepArch_Add =  { 0x00, 0x00, 0x40, 0x00 };
        private Byte[] contract_edit =          { 0x00, 0x00, 0x80, 0x00 };

        public AuthorityUtil()
        {
            AuthorityList.Add(logisticsBuy_show);
            AuthorityList.Add(logsticsBuy_add);
            AuthorityList.Add(logsticsBuy_del);
            AuthorityList.Add(logsticsBuy_update);

            AuthorityList.Add(config_show);
            AuthorityList.Add(config_add);
            AuthorityList.Add(config_update);
            AuthorityList.Add(config_del);

            AuthorityList.Add(logisticsSell_show);
            AuthorityList.Add(logisticsSell_add);
            AuthorityList.Add(logisticsSell_del);
            AuthorityList.Add(logisticsSell_update);

            AuthorityList.Add(logisticsSell_check);
            AuthorityList.Add(contract_keepArch);
            AuthorityList.Add(contract_keepArch_Add);
            AuthorityList.Add(contract_edit);
        }

        /// <summary>
        /// 对外提供的添加权限方法
        /// </summary>
        /// <param name="MyAuthorityStr">用户类的权限字段</param>
        /// <param name="AuthorityIndex">要添加的权限枚举</param>
        /// <returns></returns>
        public String AddAuthority(String MyAuthorityStr, Authority AuthorityIndex)
        {
            if (MyAuthorityStr == null)
                MyAuthorityStr = PrimaryString;

            Byte[] myAuthority = returnByte(MyAuthorityStr);
            Byte[] addAuthority = AuthorityList[(int)AuthorityIndex];
            Byte[] newAuthority = add(myAuthority,addAuthority);

            return BytesToString(newAuthority);
        }

        /// <summary>
        /// 对外提供的检验权限方法
        /// </summary>
        /// <param name="MyAuthorityStr">用户类的权限字段</param>
        /// <param name="AuthorityIndex">要检验的权限枚举</param>
        /// <returns></returns>
        public Boolean CheckAuthority(String MyAuthorityStr, Authority AuthorityIndex)
        {
            if (MyAuthorityStr == null)
                MyAuthorityStr = PrimaryString;

            Byte[] myAuthority = returnByte(MyAuthorityStr);
            Byte[] checkAuthority = AuthorityList[(int)AuthorityIndex];

            return check(myAuthority, checkAuthority);
        }

        /// <summary>
        /// 对外提供的删除权限方法
        /// </summary>
        /// <param name="MyAuthorityStr">用户类的权限字段</param>
        /// <param name="AuthorityIndex">要删除的权限枚举</param>
        /// <returns></returns>
        public String RemoveAuthority(String MyAuthorityStr, Authority AuthorityIndex)
        {
            if (MyAuthorityStr == null)
                MyAuthorityStr = PrimaryString;

            Byte[] myAuthority = returnByte(MyAuthorityStr);
            Byte[] removeAuthority = AuthorityList[(int)AuthorityIndex];

            Byte[] newAuthority = remove(myAuthority, removeAuthority);

            return BytesToString(newAuthority);
        }

        /// <summary>
        /// 内部方法,将Byte[]数组转成字符串
        /// </summary>
        /// <param name="Authority"></param>
        /// <returns></returns>
        private String BytesToString(Byte[] Authority)
        {
            String Str = "";
            for (int i = 0; i < Authority.Length; i++)
            {
                if (Authority[i]<16)
                    Str += "0" + Convert.ToString(Authority[i], 16);   //值小于16时要在数值前添0
                else
                    Str += Convert.ToString(Authority[i], 16);
            }
            return Str;
        }

        /// <summary>
        /// 内部方法,将字符串转成Byte[]数组
        /// </summary>
        /// <param name="AuthorityStr"></param>
        /// <returns></returns>
        private Byte[] returnByte(String AuthorityStr)
        {
            Char[] CharArray = AuthorityStr.ToCharArray();
            Byte[] StrByte = new Byte[CharArray.Length/2];
            
            for (int i = 0; i < CharArray.Length; i+=2)
            {
                StrByte[i / 2] = Convert.ToByte(AuthorityStr.Substring(i, 2), 16);  //Char数组中取两个字符转成16进制数
            }
            return StrByte;
        }

        /// <summary>
        /// 内部方法,验证权限
        /// </summary>
        /// <param name="myAuthority"></param>
        /// <param name="checkAuthority"></param>
        /// <returns></returns>
        private Boolean check(Byte[] myAuthority, Byte[] checkAuthority)
        {
            Boolean flag = true;

            for (int i = 0; i < myAuthority.Length; i++)
            {
                if ((Byte)(myAuthority[i] & checkAuthority[i]) != checkAuthority[i])
                    flag = false;
            }

            return flag;
        }

        /// <summary>
        /// 内部方法,增加权限
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        private Byte[] add(Byte[] a,Byte[] b)
        {
            Byte[] sum = { 0x00, 0x00, 0x00, 0x0 };

            for (int i = 0; i < a.Length; i++)
            {
                sum[i] = (Byte)(a[i] | b[i]);
            }

            return sum;
        }

        /// <summary>
        /// 内部方法,删除权限
        /// </summary>
        /// <param name="myAuthority"></param>
        /// <param name="deleteAuthority"></param>
        /// <returns></returns>
        private Byte[] remove(Byte[] myAuthority, Byte[] deleteAuthority)
        {
            for (int i = 0; i < myAuthority.Length; i++)
            {
                myAuthority[i] = (Byte)(myAuthority[i] & (~deleteAuthority[i]));
            }
            return myAuthority;
        }

    }
}
View Code

权限枚举Authority

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringByte
{
    public enum Authority
    {
        logisticsBuy_show = 0,
        logsticsBuy_add = 1,
        logsticsBuy_del = 2,
        logsticsBuy_update = 3,
        config_show = 4,
        config_add = 5,
        config_update = 6,
        config_del = 7,
        logisticsSell_show = 8,
        logisticsSell_add = 9,
        logisticsSell_del = 10,
        logisticsSell_update = 11,
        logisticsSell_check = 12,
        contract_keepArch = 13,
        contract_keepArch_Add = 14,
        contract_edit = 15,
        contract_del = 16
    }
}
View Code

用户类User

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringByte
{
    public class User
    {
        public String name { get; set; }
        public int age { get; set; }
        public String Authority { get; set; }

        public User(String name, int age)
        {
            this.name = name;
            this.age = age;
        }
    }
}

测试类Program

原文地址:https://www.cnblogs.com/xiayangqiushi/p/3366390.html