8位一组压缩字符串

若字符串都是由asiic码中的字符组成,我们可以把一个字符转成8位来表示,比如a的ascii码是97,对应的二进制是0110,0001,所以我们可以用一个int(32位)来表示4个字符(char),或者64位的int表示8个char。在C#中一个char是16位Unicode 字符,这样,可以省一半的空间。当然这只是个trick,或者当个练笔也不错。
下面是代码实现:

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 



namespace StringCompresser 

{ 

    public static class ExtentionString 

    { 

        public static int[] ToInt32(this string url) 

        { 

            if(string.IsNullOrEmpty(url)) 

            { 

                return null; 

            } 



            // 

            //we can use an 8-bit to present a char. So we can use an integer to present 4 char. 

            // 

            int arraylen = url.Length >> 2; 

             

            // 

            //if url length < 4 char. We only use one integer. Or if more char left after dividing by 4 characters. 

            //The length of return int[] must enlarge 1 

            // 

            if(arraylen==0 || url.Length % 4 > 0) 

            { 

                arraylen++; 

            } 

             



            int[] rtnArray = new int[arraylen]; 

             

             

            //1234,5678,90 



            //fill the first part of return array. That is 1234,5678 

            for (int i = 0; i < url.Length >> 2; i++) 

            { 

                int val1 = (int)url[i << 2]; 

                int val2 = (int)url[(i << 2) + 1]; 

                int val3 = (int)url[(i << 2) + 2]; 

                int val4 = (int)url[(i << 2) + 3]; 



                rtnArray[i] = (val1 << 24) | (val2 << 16) | (val3 << 8) | val4; 

            } 





            //fill the second part of return array. That is 90 

            for (int i = url.Length % 4; i >0; i--) 

            { 

                rtnArray[arraylen - 1] |= (url[url.Length - i] << ((i-1) << 2)); 

            } 



            return rtnArray; 

        } 



        public static long[] ToInt64(this string url) 

        { 

            if (string.IsNullOrEmpty(url)) 

            { 

                return null; 

            } 



            // 

            //we can use an 8-bit to present a char. So we can use an long to present 8 char. 

            // 

            int arraylen = url.Length >> 3; 



            // 

            //if url length < 8 char. We only use one integer. Or if more char left after dividing by 8 characters. 

            //The length of return int[] must enlarge 1 

            // 

            if (arraylen == 0 || url.Length % 8 > 0) 

            { 

                arraylen++; 

            } 





            long[] rtnArray = new long[arraylen]; 





            //1234,5678,9012,3456,1234,5678 



            //fill the first part of return array. That is 1234,5678,9012,3456 

            for (int i = 0; i < url.Length >> 3; i++) 

            { 

                long val1 = (long)url[i << 3]; 

                long val2 = (long)url[(i << 3) + 1]; 

                long val3 = (long)url[(i << 3) + 2]; 

                long val4 = (long)url[(i << 3) + 3]; 

                long val5 = (long)url[(i << 3) + 4]; 

                long val6 = (long)url[(i << 3) + 5]; 

                long val7 = (long)url[(i << 3) + 6]; 

                long val8 = (long)url[(i << 3) + 7]; 



                rtnArray[i] = (val1 << 56) | (val2 << 48) | (val3 << 40) | (val4 << 32) 

                    | (val5 << 24) | (val6 << 16) | (val7<<8) | val8; 

            } 



            //fill the second part of return array. That is 1234,5678 

            for (int i = url.Length % 8; i > 0; i--) 

            { 

                rtnArray[arraylen - 1] |= (url[url.Length - i] << ((i - 1) << 3)); 

            } 



            return rtnArray; 

        } 

    } 

}



可以这样调用string str = "abcd"; int[] array = str.ToInt32();

你可能感兴趣的:(字符串)