-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPMessageUtils.cs
157 lines (132 loc) · 5.08 KB
/
UDPMessageUtils.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace shvFT991A
{
class UDPMessageUtils
{
public int gIndex;
//------------------------------------------------------------------------------------------
public int Unpack1int(byte[] bData, string VarName)
{
byte b = bData[gIndex];
int retValue = Convert.ToInt32(b);
gIndex = gIndex + 1;
Console.WriteLine("Unpack1int {0} {1} {2}", VarName, gIndex, retValue);
return retValue;
}
public uint Unpack4uint(byte[] bData, string VarName)
{
byte[] b = bData.GetSegment(gIndex, 4).ToArray();
if (BitConverter.IsLittleEndian)
{
Array.Reverse(b);
}
uint retValue = BitConverter.ToUInt32(b, 0);
gIndex = gIndex + 4;
Console.WriteLine("Unpack4uint {0} {1} {2} {3}", VarName, gIndex, retValue, BitConverter.ToString(b));
return retValue;
}
public int Unpack4int(byte[] bData, string VarName)
{
byte[] b = bData.GetSegment(gIndex, 4).ToArray();
if (BitConverter.IsLittleEndian)
{
Array.Reverse(b);
}
int retValue = BitConverter.ToInt32(b, 0);
gIndex = gIndex + 4;
Console.WriteLine("Unpack4int {0} {1} {2} {3}", VarName, gIndex, retValue, BitConverter.ToString(b));
return retValue;
}
public UInt64 Unpack8uint(byte[] bData, string VarName)
{
byte[] b = bData.GetSegment(gIndex, 8).ToArray();
if (BitConverter.IsLittleEndian)
{
Array.Reverse(b);
}
UInt64 retValue = BitConverter.ToUInt64(b, 0);
gIndex = gIndex + 8;
Console.WriteLine("Unpack8uint {0} {1} {2} {3}", VarName, gIndex, retValue, BitConverter.ToString(b));
return retValue;
}
public ulong Unpack8ulong(byte[] bData, string VarName)
{
byte[] b = bData.GetSegment(gIndex, 8).ToArray();
if (BitConverter.IsLittleEndian)
{
Array.Reverse(b);
}
ulong retValue = BitConverter.ToUInt64(b, 0);
gIndex = gIndex + 8;
Console.WriteLine("Unpack8ulong {0} {1} {2} {3}", VarName, gIndex, retValue, BitConverter.ToString(b));
return retValue;
}
public float Unpack8float(byte[] bData, string VarName)
{
byte[] bb = bData.GetSegment(gIndex, 8).ToArray();
if (BitConverter.IsLittleEndian)
{
Array.Reverse(bb);
}
double d = BitConverter.Int64BitsToDouble(BitConverter.ToInt64(bb, 0));
Console.WriteLine("double = {0}", d);
float retValue = (float)d;
gIndex = gIndex + 8;
Console.WriteLine("Unpack8float {0} {1} {2} ", VarName, gIndex, retValue /*, BitConverter.ToString(b)*/);
return retValue;
}
public string Unpackstring(byte[] bData, string VarName)
{
int iNum = Unpack4int(bData,"string Num");
if (0 < iNum)
{
byte[] b = bData.GetSegment(gIndex, (int)iNum).ToArray();
if (BitConverter.IsLittleEndian)
{
//Array.Reverse(b);
}
string retValue = Encoding.UTF8.GetString(b);
gIndex = gIndex + retValue.Length;
Console.WriteLine("Unpackstring {0} {1} {2} {3}", VarName, gIndex, retValue, BitConverter.ToString(b));
return retValue;
}
else
{
Console.WriteLine("Unpackstring {0} {1} ", VarName, gIndex);
}
return "";
}
public bool Unpackbool(byte[] bData, string VarName)
{
byte[] b = bData.GetSegment(gIndex, 1).ToArray();
if (BitConverter.IsLittleEndian)
{
Array.Reverse(b);
}
bool retValue = BitConverter.ToBoolean(b, 0);
gIndex = gIndex + 1;
Console.WriteLine("Unpackbool {0} {1} {2} {3}", VarName, gIndex, retValue, BitConverter.ToString(b));
return retValue;
}
public DateTime UnpackDateTime(byte[] bData, string VarName)
{
byte[] b = bData.GetSegment(gIndex, 4).ToArray();
if (BitConverter.IsLittleEndian)
{
Array.Reverse(b);
}
int mill = BitConverter.ToInt32(b, 0);
DateTime t = DateTime.UtcNow;
DateTime date1 = new DateTime(t.Year, t.Month, t.Day, 0, 0, 0);
DateTime date2 = date1.AddMilliseconds(mill);
gIndex = gIndex + 4;
Console.WriteLine("UnpackDateTime {0} {1} {2} {3}", gIndex, date2.ToLocalTime(),date2.ToLongTimeString(), BitConverter.ToString(b));
return date2;
}
}
}