0%

数据转换

IEEE to Double、Char、int

在线转换工具

http://www.binaryconvert.com

C# IEEE to Single

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
private Single IEEE2Single(byte[] b)
{
try
{
Array.Reverse(b);
Single d = BitConverter.ToSingle(b, 0);
return (Single)Math.Round(d, 5);
}
catch (System.Exception ex)
{
return 0;
}
}
private byte[] SingleToIEEE(Single d)
{
try
{
byte[] b = new byte[]{};
b = BitConverter.GetBytes(d);
Array.Reverse(b);
return b;
}
catch (System.Exception ex)
{
return new byte[] { 0x00, 0x00, 0x00, 0x00};
}
}

3CA97E13->0.02069

格式转换

Double to Hex

1
2
3
4
5
int Height_LF = (int)Convert.ToDouble(XmlFO.ListObject["WBH_LF"].xValue);
Height_RR *= 4;
string s = string.Empty;
s = s + ((byte)(Height_LF >> 8)).ToString("x2")+ " ";
s = s + ((byte)(Height_LF)).ToString("x2") + " ";

2700->0a 8c

Byte to uint

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
uint BytesToUint(byte[] bys)
{
uint ReVal = 0;
foreach (byte b in bys)
{
ReVal <<= 8;
ReVal += b;
}
return ReVal;
}

byte[] UintToBytes(uint UnVal, int Count)
{
byte[] ReVal = new byte[Count];
for (int i = 0; i < Count; i++)
{
ReVal[Count - i - 1] = (byte)(UnVal >> (i * 8));
}
return ReVal;
}

Int to Byte

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
public xState ToByte(string sPara)
{
try
{
string[] SepPara;
if (!XmlFO.SepParams(sPara, 2, out SepPara))
return xState.xError;

xBytes tmpbyte = (xBytes)XmlFO.GetCheckSpecTypeObject(SepPara[0], typeof(xBytes));
if (tmpbyte == null)
return xState.xError;

int Count = Convert.ToInt32(SepPara[1]);
if (Count > 4) Count = 4;
if (Count < 1) Count = 1;
Byte[] TmpByte = new byte[Count];
for (int i = 0; i < Count; i++)
{
TmpByte[i] = (byte)((iValue >> ((Count - i - 1) * 8)) & 0xFF);
}
tmpbyte.Bytes = TmpByte;
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xError;
}
}

Byte

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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
public byte[] xHexToBytes(string InStr)
{
if (!InStr.StartsWith("0x"))
return System.Text.Encoding.GetEncoding(cPage).GetBytes(InStr);
InStr = InStr.Substring(2);
string NSpaceStr = InStr.Replace(" ", string.Empty);

List<byte> ReByte = new List<byte>();
for (int i = 0; i < NSpaceStr.Length / 2; i++)
{
ReByte.Add(Convert.ToByte(NSpaceStr.Substring(i * 2, 2), 16));
}
return ReByte.ToArray();
}
public byte[] StringAnaToBytes(string StrAna)
{
List<byte> ReByte = new List<byte>();
try
{
string[] strs = StrAna.Split('{', '}');
for (int i = 0; i < strs.Length; i++)
{
if (i % 2 == 0)
ReByte.AddRange(System.Text.Encoding.GetEncoding(cPage).GetBytes(strs[i]));
else ReByte.AddRange(xHexToBytes(strs[i]));
}
}
catch (Exception e)
{
string xError = string.Format("字符串转换到BYTE失败Error:{0} 字串:{1}", e.Message, StrAna);
xErrorOut(MethodInfo.GetCurrentMethod().Name, xError);
}
return ReByte.ToArray();
}
public string PackBytes(byte[] ArrByte)
{
string ReStr = "{0x";
foreach (byte b in ArrByte)
{
ReStr += b.ToString("X2");
}
ReStr += "}";
return ReStr;
}
public int cPage = Encoding.Default.CodePage;
public byte[] StringToBytes(string InValue)
{
string str = XmlFO.StringAnalysis(InValue);
return StringAnaToBytes(str);
}
public string BytesToString(byte[] InBytes)
{
return System.Text.Encoding.GetEncoding(cPage).GetString(Bytes);
}
public xState ToString(string sPara)
{
try
{
XmlFO.ListObject[sPara].xValue = BytesToString(Bytes);
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xError;
}
}
public xState ToInt(string sPara)
{
try
{
xInt TmpInt = (xInt)XmlFO.GetCheckSpecTypeObject(sPara, typeof(xInt));
if (TmpInt == null)
return xState.xError;

int Count = Bytes.Length;
int NumTmp = 0;
if (Count > 0)
{
NumTmp = (sbyte)Bytes[0];
if (Count > 4) Count = 4;
for (int i = 1; i < Count; i++)
{
NumTmp <<= 8;
NumTmp += Bytes[i];
}
}
TmpInt.iValue = NumTmp;
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xError;
}
}
public xState ToInt_MinusSign(string sPara)
{
try
{
xInt TmpInt = (xInt)XmlFO.GetCheckSpecTypeObject(sPara, typeof(xInt));
if (TmpInt == null)
return xState.xError;

int Count = Bytes.Length;
int NumTmp = 0;
if (Count > 0)
{
NumTmp = (Bytes[0] & 0x7F);
if (Count > 4) Count = 4;
for (int i = 1; i < Count; i++)
{
NumTmp <<= 8;
NumTmp += Bytes[i];
}
if (Bytes[0] > 0x7F)
NumTmp = -NumTmp;
}
TmpInt.iValue = NumTmp;
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xError;
}
}
public xState ToInt_Uint(string sPara)
{
try
{
xInt TmpInt = (xInt)XmlFO.GetCheckSpecTypeObject(sPara, typeof(xInt));
if (TmpInt == null)
return xState.xError;

int Count = Bytes.Length;
int NumTmp = 0;
if (Count > 0)
{
NumTmp = Bytes[0];
if (Count > 4) Count = 4;
for (int i = 1; i < Count; i++)
{
NumTmp <<= 8;
NumTmp += Bytes[i];
}
}
TmpInt.iValue = NumTmp;
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xError;
}
}
public xState CopyFrom(string sPara)
{
try
{
xBytes TmpByte = (xBytes)XmlFO.GetCheckSpecTypeObject(sPara, typeof(xBytes));
if (TmpByte == null)
return xState.xError;

if (TmpByte.cPage != cPage)
Bytes = Encoding.Convert(Encoding.GetEncoding(TmpByte.cPage), Encoding.GetEncoding(cPage), TmpByte.Bytes);
else Bytes = TmpByte.Bytes.ToArray();
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xError;
}
}
public xState SubByteFrom(string sPara)
{
try
{
string[] SepPara;
if (!XmlFO.SepParams(sPara, 3, out SepPara))
return xState.xError;

List<Byte> tmpByte = new List<byte>();
tmpByte.AddRange(StringToBytes(SepPara[0]));
if (tmpByte.Count > 0)
{
int start = Convert.ToInt32(SepPara[1]);//start 0
int leng = Convert.ToInt32(SepPara[2]);
if (start >= tmpByte.Count)
start = tmpByte.Count - 1;
if ((start + leng) > tmpByte.Count)
leng = tmpByte.Count - start;
Bytes = tmpByte.GetRange(start, leng).ToArray();
}
else Bytes = tmpByte.ToArray();
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xError;
}
}
public xState IsEqual(string sPara)
{
try
{
string[] SepPara;
if (!XmlFO.SepParams(sPara, 2, out SepPara))
return xState.xError;

byte[] tmpByte = StringToBytes(SepPara[0]);
int start = Convert.ToInt32(SepPara[1]);//start 0
if ((start + tmpByte.Length) > Bytes.Length)
return xState.xFalse;
for (int i = 0; i < tmpByte.Length; i++)
{
if (tmpByte[i] != Bytes[start + i])
return xState.xFalse;
}
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xError;
}
}
public xState CheckBit(string sPara)
{
try
{
string[] SepPara;
if (!XmlFO.SepParams(sPara, 2, out SepPara))
return xState.xError;

int byteNo = Convert.ToInt32(SepPara[0]);//start 0
int bitNo = Convert.ToInt32(SepPara[1]);//start 7---0
if (bitNo > 7) bitNo = 7;
if (bitNo < 0) bitNo = 0;
int Num = (Bytes[byteNo] << (7 - bitNo)) & 0xFF;
Num = (Num >> 7) & 0xFF;
if (Num == 1)
return xState.xTrue;
else return xState.xFalse;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xError;
}
}
public xState UseDefaultCodePage(string sPara)
{
cPage = Encoding.Default.CodePage;
return xState.xTrue;
}
public xState UseUTF8CodePage(string sPara)
{
cPage = Encoding.UTF8.CodePage;
return xState.xTrue;
}
public xState UseUnicodeCodePage(string sPara)
{
cPage = Encoding.Unicode.CodePage;
return xState.xTrue;
}

//转换
public xState Convert_String_ASCII(string sPara)
{
try
{
cSepParams Sep = new cSepParams(sPara, Bytes.Length);

XmlFO.ListObject[Sep.sName].xValue = Encoding.ASCII.GetString(Bytes, Sep.iStart, Sep.iCount);
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xFalse;
}
}
public xState Convert_String_BCD(string sPara)
{
try
{
cSepParams Sep = new cSepParams(sPara, Bytes.Length);

string strTmp = string.Empty;
for (int i = Sep.iStart; (i < Bytes.Length) && (i < (Sep.iStart + Sep.iCount)); i++)
strTmp += Bytes[i].ToString("X2");
XmlFO.ListObject[Sep.sName].xValue = strTmp;
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xFalse;
}
}
public xState Convert_Double(string sPara)
{
try
{
cSepParams Sep = new cSepParams(sPara, 1);
int NumTmp = 0;
for (int i = Sep.iStart; (i < Bytes.Length) && (i < (Sep.iStart + Sep.iCount)); i++)
{
NumTmp <<= 8;
NumTmp += Bytes[i];
}
double DoubTmp = (NumTmp * Sep.dRadio + Sep.dOffset);

XmlFO.ListObject[Sep.sName].xValue = DoubTmp.ToString();
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xFalse;
}
}
public xState Convert_Double_Bit(string sPara)
{
try
{
cSepParams Sep = new cSepParams(sPara, 1);
double NumTmp = 0;
if (Sep.iCount > 4)
NumTmp = BitConverter.ToDouble(Bytes, Sep.iStart);
else NumTmp = BitConverter.ToSingle(Bytes, Sep.iStart);

double DoubTmp = (NumTmp * Sep.dRadio + Sep.dOffset);

XmlFO.ListObject[Sep.sName].xValue = DoubTmp.ToString();
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xFalse;
}
}
public xState Convert_Double_Minus(string sPara)
{
try
{
cSepParams Sep = new cSepParams(sPara, 1);
int NumTmp = (Bytes[0] & 0x7F);
for (int i = (Sep.iStart + 1); (i < Bytes.Length) && (i < (Sep.iStart + Sep.iCount)); i++)
{
NumTmp <<= 8;
NumTmp += Bytes[i];
}
if (Bytes[0] > 0x7F)
NumTmp = -NumTmp;
double DoubTmp = (NumTmp * Sep.dRadio + Sep.dOffset);

XmlFO.ListObject[Sep.sName].xValue = DoubTmp.ToString();
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xFalse;
}
}

public xState Convert_Int(string sPara)
{
try
{
cSepParams Sep = new cSepParams(sPara, 1);
int NumTmp = 0;
for (int i = Sep.iStart; (i < Bytes.Length) && (i < (Sep.iStart + Sep.iCount)); i++)
{
NumTmp <<= 8;
NumTmp += Bytes[i];
}
NumTmp = (int)(NumTmp * Sep.dRadio + Sep.dOffset);

XmlFO.ListObject[Sep.sName].xValue = NumTmp.ToString();
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xFalse;
}
}
public xState Convert_Int_Bit(string sPara)
{
try
{
cSepParams Sep = new cSepParams(sPara, 1);
int NumTmp = 0;
if (Sep.iCount > 4)
NumTmp = (int)BitConverter.ToInt64(Bytes, Sep.iStart);
else if (Sep.iCount > 2)
NumTmp = (int)BitConverter.ToInt32(Bytes, Sep.iStart);
else if (Sep.iCount > 1)
NumTmp = (int)BitConverter.ToInt16(Bytes, Sep.iStart);
else NumTmp = Bytes[Sep.iStart];

NumTmp = (int)(NumTmp * Sep.dRadio + Sep.dOffset);

XmlFO.ListObject[Sep.sName].xValue = NumTmp.ToString();
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xFalse;
}
}

public xState Append_Double(string sPara)
{
try
{
cSepParams Sep = new cSepParams(sPara, 4);
int iTmp = (int)(Convert.ToDouble(XmlFO.ListObject[Sep.sName].xValue) * Sep.dRadio + Sep.dOffset);

List<Byte> ListTmp = new List<byte>();
ListTmp.AddRange(Bytes);
for (int i = 0; i < Sep.iCount; i++)
{
ListTmp.Add((byte)((iTmp >> ((Sep.iCount - i - 1) * 8)) & 0xFF));
}

Bytes = ListTmp.ToArray();
return xState.xTrue;
}
catch (Exception e)
{
xErrorOutException(e);
return xState.xFalse;
}
}