数据传输时用到的一些方法

        #region 用回车键替换Tab
        private void AccessoryInfo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if ((this.ActiveControl is TextBox))
                    SendKeys.Send("{TAB}");
                e.Handled = true;
            }
        }
        #endregion

 

 

//字符串右对齐不够2位的用'0'填充
PadLeft(2, '0')

//在类前面加 [Serializable]表示类可以被序列化
 [Serializable]

 

 

       //压缩
     using System.IO.Compression;
        public static void Compress(Stream source, Stream dest, int size)
        {
            source.Position = 0;
            dest.Position = 0;
            using (GZipStream zipStream = new GZipStream(dest, CompressionMode.Compress, true))
            {
                byte[] buf = new byte[size];
                int len;
//把source中的内容读到buf中
                while ((len = source.Read(buf, 0, buf.Length)) > 0)
                {
//把buf中的内容写到zipStream中并压缩
                    zipStream.Write(buf, 0, len);
                }
            }
        }

 

 

        //解压
        public static void Decompress(Stream source, Stream dest, int size)
        {
            source.Position = 0;
            dest.Position = 0;
            using (GZipStream zipStream = new GZipStream(source, CompressionMode.Decompress, true))
            {
                byte[] buf = new byte[size];
                int len;
                while ((len = zipStream.Read(buf, 0, buf.Length)) > 0)
                {
                    dest.Write(buf, 0, len);
                }
            }
        }

 

 

        //把内存流写到文件
        #region WriteToFile
        public static void WriteToFile(MemoryStream mem, string fileName)
        {
            long pos = mem.Position;
            mem.Position = 0;
            using (FileStream file = new FileStream(fileName, FileMode.Create))
            {
                mem.WriteTo(file);
                file.Close();
            }
            mem.Position = pos;
            return;
        }
        #endregion

 

 

        //把字节数组写到文件
        #region WriteToFile
        public static void WriteToFile(byte[] data, string fileName)
        {
            using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
            {
                fileStream.Write(data, 0, data.Length);
                fileStream.Close();
            }
        }
        #endregion

 

 

        //序列化并压缩一个对象
        public static byte[] Pack(object obj)
        {
            byte[] ret;
            BinaryFormatter formatter = new BinaryFormatter();
            using (MemoryStream stream1 = new MemoryStream())
            {
                formatter.Serialize(stream1, obj);
                using (MemoryStream stream2 = new MemoryStream())
                {
                    Compress(stream1, stream2, 4096);
                    ret = stream2.ToArray();
                }
            }
            return ret;
        }

 

 

        //解压缩、反序列化并得到一个对象
        public static object UnPAck(byte[] data)
        {
            object obj;
            using (MemoryStream stream1 = new MemoryStream())
            {
                stream1.Write(data, 0, data.Length);
                using (MemoryStream stream2 = new MemoryStream())
                {
                    Decompress(stream1, stream2, 4096);
                    BinaryFormatter formatter = new BinaryFormatter();
                    stream2.Position = 0;
                    obj = formatter.Deserialize(stream2);
                }

            }
            return obj;
        }

 

 

        //根据指定的开始位置和长度解压和反序列化一个字节数组并返回一个对象
        public static object UnPAck(byte[] data, int offset, int len)
        {
            object obj;
            using (MemoryStream stream1 = new MemoryStream())
            {
                stream1.Write(data, offset, len);
                using (MemoryStream stream2 = new MemoryStream())
                {
                    Decompress(stream1, stream2, 4096);
                    BinaryFormatter formatter = new BinaryFormatter();
                    stream2.Position = 0;  
                    obj = formatter.Deserialize(stream2);
                }

            }
            return obj;
        }

 

 

   //计算字符串的MD5码
        public static string MD5Digest(string str)
        {
            string pwd = "";
            if (str != "")
            {
                MD5 inst = MD5.Create();

                // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
                byte[] s = inst.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
                // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得

                for (int i = 0; i < s.Length; i++)
                {
                    // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符

                    pwd = pwd + s[i].ToString("X2");
                }
            }
            return pwd;
        }

        //X2中的X表示十六进制,2表示每次按2位输出,例如:两个数10和26,正常情况十六进制显示0xA、0x1A,这样看起来不整齐,为了好看,我们可以指定X2,这样显示出来就是:0x0A、0x1A。

 

 

        //计算一字节数组的MD5码
        public static string MD5Digest(byte[] data)
        {
            string pwd = "";
            if (data.Length > 0)
            {
                MD5 inst = MD5.Create();
                byte[] s = inst.ComputeHash(data);
                for (int i = 0; i < s.Length; i++)
                {
                    // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符

                    pwd = pwd + s[i].ToString("X2");
                }
            }
            return pwd;
        }

 

 

        //根据指定的开始位置和长度就算以个字节数组的MD5码
        public static string MD5Digest(byte[] data, int offset, int len)
        {
            string pwd = "";
            if (data.Length > 0)
            {
                MD5 inst = MD5.Create();
                byte[] s = inst.ComputeHash(data, offset, len);
                for (int i = 0; i < s.Length; i++)
                {
                    // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符

                    pwd = pwd + s[i].ToString("X2");
                }
            }
            return pwd;
        }

  

 

        //对称加密
        public static string EncryptDiy(string a_strString, string a_strKey)
        {
            try
            {
                byte[] keyArr = System.Text.Encoding.UTF8.GetBytes(a_strKey);

                byte key1 = 0;
                for (int i = 0; i < keyArr.Length; i++)
                {
                    key1 = (byte)(key1 ^ (keyArr[i]));
                }

                byte[] valArr = System.Text.Encoding.UTF8.GetBytes(a_strString);
                for (int i = 0; i < valArr.Length; i++)
                {
                    valArr[i] = (byte)((valArr[i]) ^ key1);
                }

                return Convert.ToBase64String(valArr);
            }
            catch
            {
                return "";
            }
        }

 

  

        //对称解密
        public static string DecryptDiy(string a_strString, string a_strKey)
        {
            try
            {
                byte[] keyArr = System.Text.Encoding.UTF8.GetBytes(a_strKey);

                byte key1 = 0;
                for (int i = 0; i < keyArr.Length; i++)
                {
                    key1 = (byte)(key1 ^ (keyArr[i]));
                }

                byte[] valArr = Convert.FromBase64String(a_strString);
                for (int i = 0; i < valArr.Length; i++)
                {
                    valArr[i] = (byte)(key1 ^ (valArr[i]));
                }

                return System.Text.Encoding.UTF8.GetString(valArr);
            }
            catch
            {
                return "";
            }
        }

 

 

        //保留2为小数(四舍五入)
        private int GetIntVal(string str)
        {
            try
            {
                const int digit = 2;
                double value = (double)Convert.ToDecimal(str);
                double vt = Math.Pow(10, digit);
                double vx = value * vt;

                vx += 0.5;
                return (int)Math.Floor(vx);
            }
            catch
            {
                return 0;
            }
        }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值