yzh
2022-05-10 a027508b5818236d0a46bc7036394c63978a68b0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
 
namespace Common
{
    public abstract class DataHelper
    {
        private static object Md5Mutex = new object();
 
        public static void SerializeDataTableXmlFile(DataTable table, string filename)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataTable));
            FileInfo fileInfo = new FileInfo(filename);
            if (!fileInfo.Directory.Exists)
                fileInfo.Directory.Create();
            using (StreamWriter streamWriter = new StreamWriter((Stream)fileInfo.Open(FileMode.OpenOrCreate)))
            {
                try
                {
                    xmlSerializer.Serialize((TextWriter)streamWriter, (object)table);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                }
            }
        }
 
        public static int MemoryCompare(byte[] b1, byte[] b2)
        {
            int num = 0;
            if (b1.Length != b2.Length)
            {
                num = b1.Length - b2.Length;
            }
            else
            {
                for (int index = 0; index < b1.Length; ++index)
                {
                    if ((int)b1[index] != (int)b2[index])
                    {
                        num = (int)b1[index] - (int)b2[index];
                        break;
                    }
                }
            }
            return num;
        }
 
        public static string MD5Hex(string source)
        {
            lock (DataHelper.Md5Mutex)
                return BitConverter.ToString(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(source))).Replace("-", "");
        }
 
        public static string MD5Hex(byte[] source, int offset, int count)
        {
            lock (DataHelper.Md5Mutex)
                return BitConverter.ToString(MD5.Create().ComputeHash(source, offset, count)).Replace("-", "");
        }
 
        public static byte[] MD5Bytes(string source)
        {
            lock (DataHelper.Md5Mutex)
                return MD5.Create().ComputeHash(Encoding.Default.GetBytes(source));
        }
 
        private static string FormatToPlainText(string text)
        {
            char[] chArray = text.ToCharArray();
            StringBuilder stringBuilder = new StringBuilder();
            foreach (char ch in chArray)
            {
                if ((int)ch >= 48 && (int)ch <= 57 || (int)ch >= 65 && (int)ch <= 90 || (int)ch >= 97 && (int)ch <= 122 || (int)ch >= 16513 && (int)ch <= 65278)
                    stringBuilder.Append(ch);
            }
            return stringBuilder.ToString();
        }
 
        public static byte[] ComputerHashBytes(string source)
        {
            lock (DataHelper.Md5Mutex)
                return MD5.Create().ComputeHash(Encoding.Default.GetBytes(source));
        }
 
        public static NameValueCollection GetParams(string exp)
        {
            NameValueCollection nameValueCollection = new NameValueCollection();
            if (!string.IsNullOrEmpty(exp))
            {
                foreach (string input in exp.Split(";\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    Match match = Regex.Match(input, "(.*)=(.*)");
                    if (match.Success)
                        nameValueCollection[match.Groups[1].Value] = match.Groups[2].Value;
                }
            }
            return nameValueCollection;
        }
 
        public static string LeftString(string s, int length)
        {
            if (string.IsNullOrEmpty(s))
                return string.Empty;
            return s.Substring(0, Math.Min(length, s.Length));
        }
 
        public static string RemovePrefixString(string source, string prefix)
        {
            try
            {
                if (source.StartsWith(prefix))
                    return source.Substring(prefix.Length);
                return source;
            }
            catch
            {
                return source;
            }
        }
 
        public static string AddPrefixString(string source, string prefix)
        {
            if (!source.StartsWith(prefix))
                return string.Format("{0}{1}", (object)prefix, (object)source);
            return source;
        }
    }
}