ASCII编码->7Bit编码

时间:2023-01-17 20:15:27

转换说明:

ASCII编码->7Bit编码

代码:

        private byte[] ASCII_2_7BIT(byte[] aryBytes)
        {
            int step = 0; //    移动字节数
            int byTemp = 0x7F;

            for (int i = 0; i < aryBytes.Length; i++)
            {
                aryBytes[i] = (byte)(aryBytes[i] & byTemp); //   每个字节最高位清0
            }
            //  拼7bit
            for (int i = 0; i < aryBytes.Length - 1; i++)
            {
                step = i % 8 + 1;

                byTemp = aryBytes[i + 1] & HexValue(step);
                aryBytes[i + 1] = (byte)(aryBytes[i + 1] >> step);
                byTemp = byTemp << (8 - step);
                aryBytes[i] = (byte)(aryBytes[i] + byTemp);

                if (step > 6) //    移动7个字节的后一个字节不再处理。
                    i++;
            }

            //  取出有效字节
            byte[] btAim = new byte[aryBytes.Length-aryBytes.Length / 8];
            for (int i = 0,j=0; i < aryBytes.Length; i++)
            {
                if ((i+1) % 8 != 0)
                    btAim[j++] = aryBytes[i];
            }

            return btAim;           
        }

 

结果验证:

 

字符串:1234

ASCII:  31 32 33 34
7BIT:    31 D9 8C 06
BIN :    00110001 11011001 10001100 00000110

 

字符串: hellohello

ASCII:   68 65 6C 6C 6F 68 65 6C 6C 6F
7BIT:     E8 32 9B FD 46 97 D9 EC 37
BIN :     11101000 00110010 10011011 11111101 01000110 10010111 11011001 11101100 00110111