-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conversion.cs
243 lines (228 loc) · 8.44 KB
/
Conversion.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
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
using System;
using System.Globalization;
namespace Sharp7
{
/// <summary>
/// Conversion methods to convert from Siemens numeric format to C# and back
/// </summary>
public static class Conversion
{
/// <summary>
/// Converts a binary string to Int32 value
/// </summary>
/// <param name="txt"></param>
/// <returns></returns>
public static int BinStringToInt32(this string txt)
{
int cnt = 0;
int ret = 0;
for (cnt = txt.Length - 1; cnt >= 0; cnt += -1)
{
if (int.Parse(txt.Substring(cnt, 1)) == 1)
{
ret += (int)(Math.Pow(2, (txt.Length - 1 - cnt)));
}
}
return ret;
}
/// <summary>
/// Converts a binary string to a byte. Can return null.
/// </summary>
/// <param name="txt"></param>
/// <returns></returns>
public static byte? BinStringToByte(this string txt)
{
int cnt = 0;
int ret = 0;
if (txt.Length == 8)
{
for (cnt = 7; cnt >= 0; cnt += -1)
{
if (int.Parse(txt.Substring(cnt, 1)) == 1)
{
ret += (int)(Math.Pow(2, (txt.Length - 1 - cnt)));
}
}
return (byte)ret;
}
return null;
}
/// <summary>
/// Converts the value to a binary string
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ValToBinString(this object value)
{
int cnt = 0;
int cnt2 = 0;
int x = 0;
string txt = "";
long longValue = 0;
try
{
if (value.GetType().Name.IndexOf("[]") < 0)
{
// ist nur ein Wert
switch (value.GetType().Name)
{
case "Byte":
x = 7;
longValue = (long)((byte)value);
break;
case "Int16":
x = 15;
longValue = (long)((Int16)value);
break;
case "Int32":
x = 31;
longValue = (long)((Int32)value);
break;
case "Int64":
x = 63;
longValue = (long)((Int64)value);
break;
default:
throw new Exception();
}
for (cnt = x; cnt >= 0; cnt += -1)
{
if (((Int64)longValue & (Int64)Math.Pow(2, cnt)) > 0)
txt += "1";
else
txt += "0";
}
}
else
{
// ist ein Array
switch (value.GetType().Name)
{
case "Byte[]":
x = 7;
byte[] ByteArr = (byte[])value;
for (cnt2 = 0; cnt2 <= ByteArr.Length - 1; cnt2++)
{
for (cnt = x; cnt >= 0; cnt += -1)
if ((ByteArr[cnt2] & (byte)Math.Pow(2, cnt)) > 0) txt += "1"; else txt += "0";
}
break;
case "Int16[]":
x = 15;
Int16[] Int16Arr = (Int16[])value;
for (cnt2 = 0; cnt2 <= Int16Arr.Length - 1; cnt2++)
{
for (cnt = x; cnt >= 0; cnt += -1)
if ((Int16Arr[cnt2] & (byte)Math.Pow(2, cnt)) > 0) txt += "1"; else txt += "0";
}
break;
case "Int32[]":
x = 31;
Int32[] Int32Arr = (Int32[])value;
for (cnt2 = 0; cnt2 <= Int32Arr.Length - 1; cnt2++)
{
for (cnt = x; cnt >= 0; cnt += -1)
if ((Int32Arr[cnt2] & (byte)Math.Pow(2, cnt)) > 0) txt += "1"; else txt += "0";
}
break;
case "Int64[]":
x = 63;
byte[] Int64Arr = (byte[])value;
for (cnt2 = 0; cnt2 <= Int64Arr.Length - 1; cnt2++)
{
for (cnt = x; cnt >= 0; cnt += -1)
if ((Int64Arr[cnt2] & (byte)Math.Pow(2, cnt)) > 0) txt += "1"; else txt += "0";
}
break;
default:
throw new Exception();
}
}
return txt;
}
catch
{
return "";
}
}
/// <summary>
/// Helper to get a bit value given a byte and the bit index.
/// Example: DB1.DBX0.5 -> var bytes = ReadBytes(DB1.DBW0); bool bit = bytes[0].SelectBit(5);
/// </summary>
/// <param name="data"></param>
/// <param name="bitPosition"></param>
/// <returns></returns>
public static bool SelectBit(this byte data, int bitPosition)
{
int mask = 1 << bitPosition;
int result = data & mask;
return (result != 0);
}
/// <summary>
/// Converts from ushort value to short value; it's used to retrieve negative values from words
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static short ConvertToShort(this ushort input)
{
short output;
output = short.Parse(input.ToString("X"), NumberStyles.HexNumber);
return output;
}
/// <summary>
/// Converts from short value to ushort value; it's used to pass negative values to DWs
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static ushort ConvertToUshort(this short input)
{
ushort output;
output = ushort.Parse(input.ToString("X"), NumberStyles.HexNumber);
return output;
}
/// <summary>
/// Converts from UInt32 value to Int32 value; it's used to retrieve negative values from DBDs
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static Int32 ConvertToInt(this uint input)
{
int output;
output = int.Parse(input.ToString("X"), NumberStyles.HexNumber);
return output;
}
/// <summary>
/// Converts from Int32 value to UInt32 value; it's used to pass negative values to DBDs
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static UInt32 ConvertToUInt(this int input)
{
uint output;
output = uint.Parse(input.ToString("X"), NumberStyles.HexNumber);
return output;
}
/// <summary>
/// Converts from double to DWord (DBD)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static UInt32 ConvertToUInt(this double input)
{
uint output;
output = Sharp7.Types.DWord.FromByteArray(Sharp7.Types.Double.ToByteArray(input));
return output;
}
/// <summary>
/// Converts from DWord (DBD) to double
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static double ConvertToDouble(this uint input)
{
double output;
output = Sharp7.Types.Double.FromByteArray(Sharp7.Types.DWord.ToByteArray(input));
return output;
}
}
}