自定义算法的压缩和解压

2015-12-07 14:50:39  访问(2641) 赞(0) 踩(0)

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Web.UI.WebControls;
using System.Xml;
using SlowX.Functions.Common;


namespace SlowX.Functions.Functions
{
    /// <summary>
    /// 自定义算法的压缩和解压
    /// </summary>
    public class ByteConvertSlowXFunctions 
    {

        /// <summary>
        /// 
        /// </summary>
        const int MAXLEN = 64;

        /// <summary>
        /// 字符串转成byte[]数组的String
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string StringToByteArrayString(string str)
        {
            if (str == null || str.Length == 0)
                return "";

            byte[] byteArray = Encoding.Default.GetBytes(str);

            return ByteArrayToString(byteArray);
        }

        /// <summary>
        /// 字符串转成byte[]数组的String
        /// </summary>
        /// <param name="str"></param>
        /// <param name="_encoding"></param>
        /// <returns></returns>
        public static string StringToByteArrayString(string str, System.Text.Encoding _encoding)
        {
            if (str == null || str.Length == 0)
                return "";

            if (_encoding == null)
            {
                throw new Exception
                   (
                       "方法:"
                       + MethodBase.GetCurrentMethod().ReflectedType.FullName
                       + " "
                       + MethodBase.GetCurrentMethod().ToString()
                       + " 发生异常:"
                       + "传入参数:System.Text.Encoding _encoding 为null。"
                   );
            }

            byte[] byteArray = _encoding.GetBytes(str);

            return ByteArrayToString(byteArray);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>
        public static string ByteArrayToString(byte[] byteArray)
        {
            if (byteArray == null)
            {
                throw new Exception
                   (
                       "方法:"
                       + MethodBase.GetCurrentMethod().ReflectedType.FullName
                       + " "
                       + MethodBase.GetCurrentMethod().ToString()
                       + " 发生异常:"
                       + "传入参数:byte[] byteArray 为null。"
                   );
            }

            StringBuilder theResult = new StringBuilder();

            int intValue = 0;
            int one = 0;
            int two = 0;

            string strItem = "";

            foreach (byte b in byteArray)
            {
                intValue = (int)b;
                one = intValue / MAXLEN;
                two = intValue % MAXLEN;

                strItem = GetChar(one).ToString() + GetChar(two).ToString();

                theResult.Append(strItem);
            }

            return theResult.ToString();
        }

        /// <summary>
        /// 字符串转成byte[]数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] StringToByteArray(string str)
        {
            if (str == null || str.Length == 0)
                return null;

            int iLen = str.Length;

            if (iLen % 2 != 0)
            {
                throw new Exception
                   (
                       "方法:"
                       + MethodBase.GetCurrentMethod().ReflectedType.FullName
                       + " "
                       + MethodBase.GetCurrentMethod().ToString()
                       + " 发生异常:"
                       + "iLen % 3 != 0,传入的字符串不是合理的byte[]值。"
                   );
            }

            int iArrayLen = iLen / 2;
            byte[] theResult = new byte[iArrayLen];

            string strItem = string.Empty;
            byte bItem = 1;
            int intValue = 0;

            for (int i = 0; i < iArrayLen; ++i)
            {
                strItem = str.Substring(i * 2, 2);

                intValue = ConvertItem(strItem);
                bItem = byte.Parse(intValue.ToString());

                theResult[i] = bItem;

            }

            return theResult;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strItem"></param>
        /// <returns></returns>
        public static int ConvertItem(string strItem)
        {
            int one = GetInt(strItem[0]);
            int two = GetInt(strItem[1]);

            return one * MAXLEN + two;
        }

        /// <summary>
        /// 获得对应的int值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static int GetInt(char input)
        {
            // 0~9 //
            if (input >= '0' && input <= '9')
                return (int)input - (int)('0');

            // 10 ~ 35 //
            if (input >= 'a' && input <= 'z')
                return (int)input - (int)('a') + 10;

            // 36 ~ 61 //
            if (input >= 'A' && input <= 'Z')
                return (int)input - (int)('A') + 36;

            if (input == '+')
                return 62;

            if (input == '-')
                return 63;

            //if (input == '*')
            //    return 64;

            //if (input == '~')
            //    return 65;

            //if (input == '<')
            //    return 66;

            //if (input == '>')
            //    return 67;

            //if (input == '{')
            //    return 68;

            //if (input == '}')
            //    return 69;

            //if (input == '[')
            //    return 70;

            //if (input == ']')
            //    return 71;

            throw new Exception("GetInt(input) = " + input.ToString() + " 未知。");
        }


        /// <summary>
        /// 获得对应的int值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static char GetChar(int input)
        {
            if (input >= 0 && input <= 9)
                return (char)(input + (int)('0'));

            if (input >= 10 && input <= 35)
                return (char)(input - 10 + (int)('a'));

            if (input >= 36 && input <= 61)
                return (char)(input - 36 + (int)('A'));

            if (input == 62)
                return '+';

            if (input == 63)
                return '-';

            //if (input == 64)
            //    return '*';

            //if (input == 65)
            //    return '~';

            //if (input == 66)
            //    return '<';

            //if (input == 67)
            //    return '>';

            //if (input == 68)
            //    return '{';

            //if (input == 69)
            //    return '}';

            //if (input == 70)
            //    return '[';

            //if (input == 71)
            //    return ']';

            throw new Exception("GetChar(input) = " + input.ToString() + " 未知。");
        } 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ByteArrayStringToString(string str)
        {
            byte[] bArray = StringToByteArray(str);

            return Encoding.Default.GetString(bArray);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="_encoding"></param>
        /// <returns></returns>
        public static string ByteArrayStringToString(string str, System.Text.Encoding _encoding)
        {
            if (_encoding == null)
            {
                throw new Exception
                   (
                       "方法:"
                       + MethodBase.GetCurrentMethod().ReflectedType.FullName
                       + " "
                       + MethodBase.GetCurrentMethod().ToString()
                       + " 发生异常:"
                       + "传入参数:System.Text.Encoding _encoding 为null。"
                   );
            }

            byte[] bArray = StringToByteArray(str);

            return _encoding.GetString(bArray);
        }

    }
}


标签:自定义算法的压缩和解压 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)