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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace Misc
{
public class Converter
{
/// <summary>
/// 为了兼容minigui GB2312 格式的 bytes 转 字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string BytesToString(byte[] bytes)
{
return Encoding.GetEncoding("GB2312").GetString(bytes);//System.Text.Encoding.UTF8.GetString(bytes);
}
/// <summary>
/// 为了兼容minigui GB2312 格式的 bytes 转 字符串
/// </summary>
/// <param name="bytes"></param>
/// <param name="index"></param>
/// <param name="count"></param>
/// <returns></returns>
public static string BytesToString(byte[] bytes, int index, int count)
{
return Encoding.GetEncoding("GB2312").GetString(bytes, index, count);// System.Text.Encoding.UTF8.GetString(bytes, index, count);
}
/// <summary>
/// 为了兼容minigui 字符串转 GB2312 格式的 bytes
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static byte[] StringToBytes(string str)
{
if (string.IsNullOrEmpty(str))
return null;
return Encoding.GetEncoding("GB2312").GetBytes(str);// System.Text.Encoding.UTF8.GetBytes(str);
}
/// <summary>
/// DateTime 转字符串,基本不用
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string DateTimeToString(DateTime dt)
{
return dt.ToString();
}
/// <summary>
/// 给 linux 的time_t 使用, DateTime 转 time_t
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static UInt32 DateTime2Time_T(DateTime dt)
{
return (UInt32)dt.Subtract(DateTime.Parse("01/01/1970")).TotalSeconds;
}
/// <summary>
/// 给 linux 的time_t 使用, time_t 转 DateTime
/// </summary>
/// <param name="time_t"></param>
/// <returns></returns>
public static DateTime Time_T2DateTime(UInt32 time_t)
{
return DateTime.Parse("01/01/1970").AddSeconds(time_t);
}
/// <summary>
/// ip port 转 bytes
/// </summary>
/// <param name="ep"></param>
/// <returns></returns>
public static byte[] IPEndPoint2Bytes(IPEndPoint ep)
{
return StringToBytes(ep.ToString());
}
/// <summary>
/// bytes 转 ip port
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static IPEndPoint Bytes2IPEndPoint(byte[] bytes)
{
string str = BytesToString(bytes);
return StringConverter.ToIPEndPoint(str);
}
/// <summary>
/// bytes 转 ip port
/// </summary>
/// <param name="bytes"></param>
/// <param name="index"></param>
/// <param name="count"></param>
/// <returns></returns>
public static IPEndPoint Bytes2IPEndPoint(byte[] bytes, int index, int count)
{
string str = BytesToString(bytes, index, count);
return StringConverter.ToIPEndPoint(str);
}
/// <summary>
/// 根据类型 字符串 转 数据
/// </summary>
/// <param name="val">字符串</param>
/// <param name="type">类型</param>
/// <returns>数据</returns>
public static object Parse(string val, Type type)
{
if (type == typeof(UInt16))
return UInt16.Parse(val);
if (type == typeof(UInt32))
return UInt32.Parse(val);
if (type == typeof(bool))
return bool.Parse(val);
if (type == typeof(Int16))
return Int16.Parse(val);
if (type == typeof(Int32))
return Int32.Parse(val);
if (type == typeof(float))
return float.Parse(val);
return null;
}
}
}