wzp
2022-10-25 1961452ebc018cf758924f26d9c7aa0b31495227
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
namespace Common
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Text.RegularExpressions;
    using System.Linq;
 
    public class DataConverter
    {
        protected DataConverter()
        {
        }
 
        public static DateTime BigIntToDateTime(long inputLong)
        {
            DateTime date = DateTime.Now.Date;
            DateTime.TryParse("1970-01-01", out date);
            double num = ((double)inputLong) / 86400.0;
            return date.AddDays(num);
        }
 
        public static string getDateDotSplit(DateTime? currentTime, string split)
        {
            string str = "待定";
            if (currentTime.HasValue)
            {
                str = string.Concat(new object[] { currentTime.Value.Year, split, currentTime.Value.Month, split, currentTime.Value.Day });
            }
            return str;
        }
 
        public static string IntToStringIP(int ip)
        {
            StructIp ip2 = new StructIp
            {
                _IntIP = ip
            };
            return (ip2._IP.ip1.ToString() + "." + ip2._IP.ip2.ToString() + "." + ip2._IP.ip3.ToString() + "." + ip2._IP.ip4.ToString());
        }
 
        public static int SafeInt32(object objNum)
        {
            if (objNum != null)
            {
                string strNumber = objNum.ToString();
                if (DataValidate.IsNumber(strNumber))
                {
                    if (strNumber.ToString().Length > 9)
                    {
                        return 0x7fffffff;
                    }
                    return int.Parse(strNumber);
                }
            }
            return 0;
        }
 
        public static bool StrToBool(string strValue)
        {
            if (!string.IsNullOrEmpty(strValue))
            {
                strValue = strValue.Trim();
                return (((string.Compare(strValue, "true", true) == 0) || (string.Compare(strValue, "yes", true) == 0)) || (string.Compare(strValue, "1", true) == 0));
            }
            return false;
        }
 
        public static DateTime StrToDateTime(object strValue, DateTime defValue)
        {
            DateTime time;
            if ((strValue == null) || (strValue.ToString().Length > 20))
            {
                return defValue;
            }
            if (!DateTime.TryParse(strValue.ToString(), out time))
            {
                time = defValue;
            }
            return time;
        }
 
        public static decimal StrToDecimal(object strValue)
        {
            if (!(Convert.IsDBNull(strValue) || object.Equals(strValue, null)))
            {
                return StrToDecimal(strValue.ToString());
            }
            return 0M;
        }
 
        public static decimal StrToDecimal(string strValue)
        {
            decimal num;
            decimal.TryParse(strValue, out num);
            return num;
        }
 
        public static decimal StrToDecimal(string input, decimal defaultValue)
        {
            decimal num;
            if (decimal.TryParse(input, out num))
            {
                return num;
            }
            return defaultValue;
        }
 
        public static double StrToDouble(object strValue)
        {
            if (!(Convert.IsDBNull(strValue) || object.Equals(strValue, null)))
            {
                return StrToDouble(strValue.ToString());
            }
            return 0.0;
        }
 
        public static double StrToDouble(string strValue)
        {
            double num;
            double.TryParse(strValue, out num);
            return num;
        }
 
        public static int StrToInt(object strValue)
        {
            if ((((strValue != null) && (strValue.ToString() != string.Empty)) && (strValue.ToString().Length <= 10)) && Regex.IsMatch(strValue.ToString(), "^[0-9]+$"))
            {
                return Convert.ToInt32(strValue.ToString());
            }
            return -1;
        }
 
        public static int StrToInt(object strValue, int defValue)
        {
            if (((strValue != null) && (strValue.ToString() != string.Empty)) && Regex.IsMatch(strValue.ToString(), "^[0-9]+$"))
            {
                return Convert.ToInt32(strValue.ToString());
            }
            return defValue;
        }
 
        public static long StrToInt64(string strValue, long defValue)
        {
            long result = 0L;
            if (long.TryParse(strValue, out result))
            {
                return result;
            }
            return defValue;
        }
 
        public static int StrToPageInt(object strValue, int defValue)
        {
            if (((strValue != null) && (strValue.ToString() != string.Empty)) && Regex.IsMatch(strValue.ToString(), "^[0-9]+$"))
            {
                int num = Convert.ToInt32(strValue.ToString());
                if (num < 1)
                {
                    num = 1;
                }
                return num;
            }
            return defValue;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        private struct IPAdress
        {
            public byte ip1;
            public byte ip2;
            public byte ip3;
            public byte ip4;
        }
 
        [StructLayout(LayoutKind.Explicit)]
        private struct StructIp
        {
            [FieldOffset(0)]
            public int _IntIP;
            [FieldOffset(0)]
            public DataConverter.IPAdress _IP;
        }
 
        //获取随机码
        public static string getRandom(int strLength)
        {
            string random = "";
            if (strLength <= 0)
            {
                strLength = 6;  //默认6位长度
            }
            //random = Guid.NewGuid().ToString().Replace("-", "").Substring(0, strLength);
            random = Guid.NewGuid().ToString();
            random = random.Replace("-", "");
            random = random.Substring(0, strLength);
 
            return random;
        }
 
        //字符串转数组
        public static object[] stringToArray(string str)
        {
            object[] array = str.Split(',');
            return array;
        }
 
        //数组转字符串
        public static string arrayToString(object[] array)
        {
            if (array == null)
                return "";
            string str = string.Join(",", array);
            return str;
        }
 
        //数组转换List:string[] str ={ "str","string","abc"}转 List<string>
        public static List<object> arrayToList(object[] array)
        {
            List<object> list = new List<object>(array);
 
            return list;
        }
 
        //List转换数组:List<string>转到string[]
        public static object[] listToArray(List<object> list)
        {
            object[] array = list.ToArray();
 
            return array;
        }
 
        //字符串转List
        public static List<object> stringToList(string str)
        {
            if (string.IsNullOrEmpty(str))
                return null;
 
            List<object> list = new List<object>();
            //字符串转数组,再数组合并
            list.AddRange(str.Split(','));
 
            return list;
        }
 
        //数组去重,移除数组中重复数据
        public static string[] DelRepeatData(string[] array)
        {
            return array.GroupBy(p => p).Select(p => p.Key).ToArray();
        }
 
        //检查数组格式字符串中是否包含某元素
        public static bool checkStrForArrayStr(object str, string arrayStr )
        {
            return stringToArray(arrayStr).Contains(str.ToString() );
        }
 
        //检查数组中是否包含某元素
        public static bool checkStrForArray(object str, object[] array)
        {
            return array.Contains(str);
        }
 
        //根据数组字符串转换字典
        public Dictionary<string, object> arrayStrToDict(object userId, string arrayStr)
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            if (string.IsNullOrEmpty(arrayStr))
                return dictionary;
 
            List<object> list = DataConverter.stringToList(arrayStr);
            for (int i = 0; i < list.Count; i++)
            {
                dictionary[userId.ToString()] = list[i];
            }
            return dictionary;
        }
 
    }
 
}