C# byte转bit bit组装成byte

bit组装成Byte

public void BitToByte()
        {
            List<char[]> chars = new List<char[]>();
            char mm1 = '1';
            char mm2 = '1';
            char mm3 = '1';
            char mm4 = '1';
            char mm = '0';
            char[] chars0 = new char[]{ mm1, mm2, mm3, mm4, mm, mm, mm, mm };//0-7 
            chars.Add(chars0);

            byte[] bytes = new byte[chars.Count];

            byte result = 0;
            byte current = 1;
            for (int j = 0; j < chars[0].Length; j++)
            {
                if (chars[0][j] == '1')
                {
                    result += current;
                }
                current *= 2;
            }
            bytes[0] = result;
        }

byte转bitarray

public void test()
{
	byte[] bytes1 = ByteArrayToHexString(arr1);
	BitArray bitValues1 = new BitArray(bytes1);//0-7
}
//
public static byte[] ByteArrayToHexString(string hexString)
        {

            //将16进制秘钥转成字节数组

            var byteArray = new byte[hexString.Length / 2];

            for (var x = 0; x < byteArray.Length; x++)
            {

                var i = Convert.ToInt32(hexString.Substring(x * 2, 2), 16);

                byteArray[x] = (byte)i;

            }
            return byteArray;

        }

你可能感兴趣的:(c#,java,javascript)