-
Notifications
You must be signed in to change notification settings - Fork 3
/
KMNumeratorForm.cs
1185 lines (1133 loc) · 46 KB
/
KMNumeratorForm.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Windows.Forms;
namespace KMZRebuilder
{
public partial class KMNumeratorForm : Form
{
private XmlDocument document = null;
private string fileName = "";
private bool isKml = false;
public KMNumeratorForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button2.Enabled = false;
OpenFileDialog dialog = new OpenFileDialog();
dialog.DefaultExt = ".gpx";
dialog.Filter = "GPX and KML files(*.gpx;*.kml)|*.gpx;*.kml|KML files (*.kml)|*.kml|GPS Exchange Format (*.gpx)|*.gpx|All files (*.*)|*.*";
dialog.FileName = this.fileName;
if (dialog.ShowDialog() == DialogResult.OK)
if (File.Exists(dialog.FileName))
{
this.fileName = dialog.FileName;
AnalyseFile();
button2.Enabled = true;
};
dialog.Dispose();
progressBar1.Value = 0;
}
private void AnalyseFile()
{
this.textBox1.Text = System.IO.Path.GetFileName(this.fileName);
string ext = System.IO.Path.GetExtension(fileName).ToLower();
document = new XmlDocument();
document.Load(fileName);
if (ext == ".kml") document.InnerXml = Regex.Replace(document.InnerXml, @"(<kml.*?>\s*)+", "<kml>", RegexOptions.Singleline);
if (ext == ".gpx") document.InnerXml = Regex.Replace(document.InnerXml, @"(<gpx.*?>\s*)+", "<gpx>", RegexOptions.Singleline);
isKml = ext == ".kml";
string[] toFind = new string[]{
"kml/Document/Placemark/MultiGeometry/LineString/coordinates",
"kml/Document/MultiGeometry/LineString/coordinates",
"kml/Document/Folder/Placemark/MultiGeometry/LineString/coordinates",
"kml/Document/Folder/MultiGeometry/LineString/coordinates",
"kml/Document/Folder/Placemark/LineString/coordinates",
"kml/Document/Folder/Placemark/Point/coordinates",
"gpx/trk/trkseg/trkpt",
"gpx/rte/rtept"
};
string txt = "";
comboBox1.Items.Clear();
foreach (string toF in toFind)
{
XmlNodeList list = document.SelectNodes(toF);
if (list.Count > 0)
{
comboBox1.Items.Add(comboBox1.Text = toF);
txt += (txt.Length > 0 ? ", " :"") + list.Count.ToString();
};
};
if (comboBox1.Items.Count > 1)
comboBox1.SelectedIndex = 0;
if (txt == "")
txt = "Nothing found";
else
txt = "Found: " + txt + " objects";
statusText.Text = txt;
}
private void button2_Click(object sender, EventArgs e)
{
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InstalledUICulture;
System.Globalization.NumberFormatInfo provider = (System.Globalization.NumberFormatInfo)ci.NumberFormat.Clone();
provider.NumberDecimalSeparator = ".";
XmlNodeList nlist = document.SelectNodes(comboBox1.Text);
if (nlist.Count == 0)
{
MessageBox.Show("No Data Found!");
return;
};
// WORK
{
List<PointF> xy = new List<PointF>();
foreach (XmlNode xn in nlist)
{
if (!isKml)
xy.Add(new PointF(float.Parse(xn.Attributes["lon"].InnerText, provider), float.Parse(xn.Attributes["lat"].InnerText, provider)));
else
{
string[] xya = xn.ChildNodes[0].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
foreach (string xye in xya)
{
string[] xyz = xye.Split(new string[] { "," }, StringSplitOptions.None);
xy.Add(new PointF(float.Parse(xyz[0], provider), float.Parse(xyz[1], provider)));
};
};
};
if (xy.Count == 0)
{
MessageBox.Show("No Data Found!");
return;
};
this.progressBar1.Maximum = xy.Count;
this.statusText.Text = "Running...";
float kmError = 0.15f;
float total_dist = 0;
int currFlag = 1;
List<KM> kmFlags = new List<KM>();
float prevLat = xy[0].Y;
float prevLon = xy[0].X;
kmFlags.Add(new KM(0, prevLat, prevLon));
for (int i = 1; i < xy.Count; i++)
{
float currLat = xy[i].Y;
float currLon = xy[i].X;
float dist_prev_curr = Utils.GetLengthMeters((double)prevLat, (double)prevLon, (double)currLat, (double)currLon, false) / 1000f;
total_dist += dist_prev_curr;
bool flag = true;
while (flag)
{
float walked = total_dist - (currFlag);
if (walked >= 0)
{
if (walked <= kmError)
{
kmFlags.Add(new KM(currFlag, currLat, currLon));
currFlag++;
}
else
{
float walkback = dist_prev_curr - walked;
float btwLat = prevLat + (((currLat - prevLat) / dist_prev_curr) * walkback);
float btwLon = prevLon + (((currLon - prevLon) / dist_prev_curr) * walkback);
kmFlags.Add(new KM(currFlag, btwLat, btwLon));
currFlag++;
}
}
else
{
flag = false;
}
}
prevLat = currLat;
prevLon = currLon;
this.progressBar1.Value = i - 1;
this.statusText.Text = string.Format("Analize points {0} of {1}, counted {2} kms of path", i - 1, xy.Count, kmFlags.Count);
this.statusText.Update();
}
this.statusText.Text = string.Format("Analized {1} point, counted {2} kms of path", 0, xy.Count, kmFlags.Count);
this.statusText.Update();
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Select output KML file";
sfd.DefaultExt = ".kml";
sfd.FileName = this.fileName.Remove(this.fileName.Length - 4) + "[km].kml";
sfd.Filter = "KML Files (*.kml)|*.kml";
if (sfd.ShowDialog() == DialogResult.OK)
{
int incKm = (int)this.incVal.Value;
int startKm = (int)this.startVal.Value;
int skipKm = (int)this.skipVal.Value;
FileStream stream = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
writer.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://earth.google.com/kml/2.2\"><Document xmlns=\"\"><name>Km Flags</name>\r\n");
writer.Write("<Folder><name>Each " + this.incVal.Text + " km</name>\r\n");
for (int i = skipKm; i < kmFlags.Count; i+=incKm)
{
KM km = kmFlags[i];
writer.Write("<Placemark><name>" + (km.km - skipKm + startKm).ToString() + " km</name><Point><coordinates>");
writer.Write(km.Lon.ToString().Replace(",", ".") + "," + km.Lat.ToString().Replace(",", ".") + ",0 ");
writer.Write("</coordinates></Point></Placemark>\r\n");
}
writer.Write("</Folder></Document></kml>");
writer.Flush();
writer.Close();
};
sfd.Dispose();
}
}
}
public class KM
{
public int km;
public float Lat;
public float Lon;
public KM(int km, float Lat, float Lon)
{
this.km = km;
this.Lat = Lat;
this.Lon = Lon;
}
}
public class Utils
{
// Ðàññ÷åò ðàññòîÿíèÿ
#region LENGTH
public static float GetLengthMeters(double StartLat, double StartLong, double EndLat, double EndLong, bool radians)
{
// use fastest
float result = (float)GetLengthMetersD(StartLat, StartLong, EndLat, EndLong, radians);
if (float.IsNaN(result))
{
result = (float)GetLengthMetersC(StartLat, StartLong, EndLat, EndLong, radians);
if (float.IsNaN(result))
{
result = (float)GetLengthMetersE(StartLat, StartLong, EndLat, EndLong, radians);
if (float.IsNaN(result))
result = 0;
};
};
return result;
}
// Slower
public static uint GetLengthMetersA(double StartLat, double StartLong, double EndLat, double EndLong, bool radians)
{
double D2R = Math.PI / 180; // Ïðåîáðàçîâàíèå ãðàäóñîâ â ðàäèàíû
double a = 6378137.0000; // WGS-84 Equatorial Radius (a)
double f = 1 / 298.257223563; // WGS-84 Flattening (f)
double b = (1 - f) * a; // WGS-84 Polar Radius
double e2 = (2 - f) * f; // WGS-84 Êâàäðàò ýêñöåíòðè÷íîñòè ýëëèïñîèäà // 1-(b/a)^2
// Ïåðåìåííûå, èñïîëüçóåìûå äëÿ âû÷èñëåíèÿ ñìåùåíèÿ è ðàññòîÿíèÿ
double fPhimean; // Ñðåäíÿÿ øèðîòà
double fdLambda; // Ðàçíèöà ìåæäó äâóìÿ çíà÷åíèÿìè äîëãîòû
double fdPhi; // Ðàçíèöà ìåæäó äâóìÿ çíà÷åíèÿìè øèðîòû
double fAlpha; // Ñìåùåíèå
double fRho; // Ìåðèäèàíñêèé ðàäèóñ êðèâèçíû
double fNu; // Ïîïåðå÷íûé ðàäèóñ êðèâèçíû
double fR; // Ðàäèóñ ñôåðû Çåìëè
double fz; // Óãëîâîå ðàññòîÿíèå îò öåíòðà ñôåðîèäà
double fTemp; // Âðåìåííàÿ ïåðåìåííàÿ, èñïîëüçóþùàÿñÿ â âû÷èñëåíèÿõ
// Âû÷èñëÿåì ðàçíèöó ìåæäó äâóìÿ äîëãîòàìè è øèðîòàìè è ïîëó÷àåì ñðåäíþþ øèðîòó
// ïðåäïîëîæèòåëüíî ÷òî ðàññòîÿíèå ìåæäó òî÷êàìè << ðàäèóñà çåìëè
if (!radians)
{
fdLambda = (StartLong - EndLong) * D2R;
fdPhi = (StartLat - EndLat) * D2R;
fPhimean = ((StartLat + EndLat) / 2) * D2R;
}
else
{
fdLambda = StartLong - EndLong;
fdPhi = StartLat - EndLat;
fPhimean = (StartLat + EndLat) / 2;
};
// Âû÷èñëÿåì ìåðèäèàííûå è ïîïåðå÷íûå ðàäèóñû êðèâèçíû ñðåäíåé øèðîòû
fTemp = 1 - e2 * (sqr(Math.Sin(fPhimean)));
fRho = (a * (1 - e2)) / Math.Pow(fTemp, 1.5);
fNu = a / (Math.Sqrt(1 - e2 * (Math.Sin(fPhimean) * Math.Sin(fPhimean))));
// Âû÷èñëÿåì óãëîâîå ðàññòîÿíèå
if (!radians)
{
fz = Math.Sqrt(sqr(Math.Sin(fdPhi / 2.0)) + Math.Cos(EndLat * D2R) * Math.Cos(StartLat * D2R) * sqr(Math.Sin(fdLambda / 2.0)));
}
else
{
fz = Math.Sqrt(sqr(Math.Sin(fdPhi / 2.0)) + Math.Cos(EndLat) * Math.Cos(StartLat) * sqr(Math.Sin(fdLambda / 2.0)));
};
fz = 2 * Math.Asin(fz);
// Âû÷èñëÿåì ñìåùåíèå
if (!radians)
{
fAlpha = Math.Cos(EndLat * D2R) * Math.Sin(fdLambda) * 1 / Math.Sin(fz);
}
else
{
fAlpha = Math.Cos(EndLat) * Math.Sin(fdLambda) * 1 / Math.Sin(fz);
};
fAlpha = Math.Asin(fAlpha);
// Âû÷èñëÿåì ðàäèóñ Çåìëè
fR = (fRho * fNu) / (fRho * sqr(Math.Sin(fAlpha)) + fNu * sqr(Math.Cos(fAlpha)));
// Ïîëó÷àåì ðàññòîÿíèå
return (uint)Math.Round(Math.Abs(fz * fR));
}
// Slowest
public static uint GetLengthMetersB(double StartLat, double StartLong, double EndLat, double EndLong, bool radians)
{
double fPhimean, fdLambda, fdPhi, fAlpha, fRho, fNu, fR, fz, fTemp, Distance,
D2R = Math.PI / 180,
a = 6378137.0,
e2 = 0.006739496742337;
if (radians) D2R = 1;
fdLambda = (StartLong - EndLong) * D2R;
fdPhi = (StartLat - EndLat) * D2R;
fPhimean = (StartLat + EndLat) / 2.0 * D2R;
fTemp = 1 - e2 * Math.Pow(Math.Sin(fPhimean), 2);
fRho = a * (1 - e2) / Math.Pow(fTemp, 1.5);
fNu = a / Math.Sqrt(1 - e2 * Math.Sin(fPhimean) * Math.Sin(fPhimean));
fz = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(fdPhi / 2.0), 2) +
Math.Cos(EndLat * D2R) * Math.Cos(StartLat * D2R) * Math.Pow(Math.Sin(fdLambda / 2.0), 2)));
fAlpha = Math.Asin(Math.Cos(EndLat * D2R) * Math.Sin(fdLambda) / Math.Sin(fz));
fR = fRho * fNu / (fRho * Math.Pow(Math.Sin(fAlpha), 2) + fNu * Math.Pow(Math.Cos(fAlpha), 2));
Distance = fz * fR;
return (uint)Math.Round(Distance);
}
// Average
public static uint GetLengthMetersC(double StartLat, double StartLong, double EndLat, double EndLong, bool radians)
{
double D2R = Math.PI / 180;
if (radians) D2R = 1;
double dDistance = Double.MinValue;
double dLat1InRad = StartLat * D2R;
double dLong1InRad = StartLong * D2R;
double dLat2InRad = EndLat * D2R;
double dLong2InRad = EndLong * D2R;
double dLongitude = dLong2InRad - dLong1InRad;
double dLatitude = dLat2InRad - dLat1InRad;
// Intermediate result a.
double a = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) +
Math.Cos(dLat1InRad) * Math.Cos(dLat2InRad) *
Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);
// Intermediate result c (great circle distance in Radians).
double c = 2.0 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a));
const double kEarthRadiusKms = 6378137.0000;
dDistance = kEarthRadiusKms * c;
return (uint)Math.Round(dDistance);
}
// Fastest
public static double GetLengthMetersD(double sLat, double sLon, double eLat, double eLon, bool radians)
{
double EarthRadius = 6378137.0;
double lon1 = radians ? sLon : DegToRad(sLon);
double lon2 = radians ? eLon : DegToRad(eLon);
double lat1 = radians ? sLat : DegToRad(sLat);
double lat2 = radians ? eLat : DegToRad(eLat);
return EarthRadius * (Math.Acos(Math.Sin(lat1) * Math.Sin(lat2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(lon1 - lon2)));
}
// Fastest
public static double GetLengthMetersE(double sLat, double sLon, double eLat, double eLon, bool radians)
{
double EarthRadius = 6378137.0;
double lon1 = radians ? sLon : DegToRad(sLon);
double lon2 = radians ? eLon : DegToRad(eLon);
double lat1 = radians ? sLat : DegToRad(sLat);
double lat2 = radians ? eLat : DegToRad(eLat);
/* This algorithm is called Sinnott's Formula */
double dlon = (lon2) - (lon1);
double dlat = (lat2) - (lat1);
double a = Math.Pow(Math.Sin(dlat / 2), 2.0) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(dlon / 2), 2.0);
double c = 2 * Math.Asin(Math.Sqrt(a));
return EarthRadius * c;
}
private static double sqr(double val)
{
return val * val;
}
public static double DegToRad(double deg)
{
return (deg / 180.0 * Math.PI);
}
#endregion LENGTH
/// <summary>
/// Converts base data types to an array of bytes, and an array of bytes to base
/// data types.
/// All info taken from the meta data of System.BitConverter. This implementation
/// allows for Endianness consideration.
///</summary>
public class MyBitConverter
{
/// <summary>
/// Constructor
/// </summary>
public MyBitConverter()
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="IsLittleEndian">Indicates the byte order ("endianess") in which data is stored in this computer architecture.</param>
public MyBitConverter(bool IsLittleEndian)
{
this.isLittleEndian = IsLittleEndian;
}
/// <summary>
/// Indicates the byte order ("endianess") in which data is stored in this computer
/// architecture.
/// </summary>
private bool isLittleEndian = true;
/// <summary>
/// Indicates the byte order ("endianess") in which data is stored in this computer
/// architecture.
///</summary>
public bool IsLittleEndian { get { return isLittleEndian; } set { isLittleEndian = value; } } // should default to false, which is what we want for Empire
/// <summary>
/// Converts the specified double-precision floating point number to a 64-bit
/// signed integer.
///
/// Parameters:
/// value:
/// The number to convert.
///
/// Returns:
/// A 64-bit signed integer whose value is equivalent to value.
///</summary>
public long DoubleToInt64Bits(double value) { throw new NotImplementedException(); }
///
/// <summary>
/// Returns the specified Boolean value as an array of bytes.
///
/// Parameters:
/// value:
/// A Boolean value.
///
/// Returns:
/// An array of bytes with length 1.
///</summary>
public byte[] GetBytes(bool value)
{
if (IsLittleEndian)
{
return System.BitConverter.GetBytes(value);
}
else
{
byte[] res = System.BitConverter.GetBytes(value);
Array.Reverse(res);
return res;
}
}
///
/// <summary>
/// Returns the specified Unicode character value as an array of bytes.
///
/// Parameters:
/// value:
/// A character to convert.
///
/// Returns:
/// An array of bytes with length 2.
///</summary>
public byte[] GetBytes(char value)
{
if (IsLittleEndian)
{
return System.BitConverter.GetBytes(value);
}
else
{
byte[] res = System.BitConverter.GetBytes(value);
Array.Reverse(res);
return res;
}
}
///
/// <summary>
/// Returns the specified double-precision floating point value as an array of
/// bytes.
///
/// Parameters:
/// value:
/// The number to convert.
///
/// Returns:
/// An array of bytes with length 8.
///</summary>
public byte[] GetBytes(double value)
{
if (IsLittleEndian)
{
return System.BitConverter.GetBytes(value);
}
else
{
byte[] res = System.BitConverter.GetBytes(value);
Array.Reverse(res);
return res;
}
}
///
/// <summary>
/// Returns the specified single-precision floating point value as an array of
/// bytes.
///
/// Parameters:
/// value:
/// The number to convert.
///
/// Returns:
/// An array of bytes with length 4.
///</summary>
public byte[] GetBytes(float value)
{
if (IsLittleEndian)
{
return System.BitConverter.GetBytes(value);
}
else
{
byte[] res = System.BitConverter.GetBytes(value);
Array.Reverse(res);
return res;
}
}
///
/// <summary>
/// Returns the specified 32-bit signed integer value as an array of bytes.
///
/// Parameters:
/// value:
/// The number to convert.
///
/// Returns:
/// An array of bytes with length 4.
///</summary>
public byte[] GetBytes(int value)
{
if (IsLittleEndian)
{
return System.BitConverter.GetBytes(value);
}
else
{
byte[] res = System.BitConverter.GetBytes(value);
Array.Reverse(res);
return res;
}
}
///
/// <summary>
/// Returns the specified 64-bit signed integer value as an array of bytes.
///
/// Parameters:
/// value:
/// The number to convert.
///
/// Returns:
/// An array of bytes with length 8.
///</summary>
public byte[] GetBytes(long value)
{
if (IsLittleEndian)
{
return System.BitConverter.GetBytes(value);
}
else
{
byte[] res = System.BitConverter.GetBytes(value);
Array.Reverse(res);
return res;
}
}
///
/// <summary>
/// Returns the specified 16-bit signed integer value as an array of bytes.
///
/// Parameters:
/// value:
/// The number to convert.
///
/// Returns:
/// An array of bytes with length 2.
///</summary>
public byte[] GetBytes(short value)
{
if (IsLittleEndian)
{
return System.BitConverter.GetBytes(value);
}
else
{
byte[] res = System.BitConverter.GetBytes(value);
Array.Reverse(res);
return res;
}
}
///
/// <summary>
/// Returns the specified 32-bit unsigned integer value as an array of bytes.
///
/// Parameters:
/// value:
/// The number to convert.
///
/// Returns:
/// An array of bytes with length 4.
///</summary>
public byte[] GetBytes(uint value)
{
if (IsLittleEndian)
{
return System.BitConverter.GetBytes(value);
}
else
{
byte[] res = System.BitConverter.GetBytes(value);
Array.Reverse(res);
return res;
}
}
///
/// <summary>
/// Returns the specified 64-bit unsigned integer value as an array of bytes.
///
/// Parameters:
/// value:
/// The number to convert.
///
/// Returns:
/// An array of bytes with length 8.
///</summary>
public byte[] GetBytes(ulong value)
{
if (IsLittleEndian)
{
return System.BitConverter.GetBytes(value);
}
else
{
byte[] res = System.BitConverter.GetBytes(value);
Array.Reverse(res);
return res;
}
}
///
/// <summary>
/// Returns the specified 16-bit unsigned integer value as an array of bytes.
///
/// Parameters:
/// value:
/// The number to convert.
///
/// Returns:
/// An array of bytes with length 2.
///</summary>
public byte[] GetBytes(ushort value)
{
if (IsLittleEndian)
{
return System.BitConverter.GetBytes(value);
}
else
{
byte[] res = System.BitConverter.GetBytes(value);
Array.Reverse(res);
return res;
}
}
///
/// <summary>
/// Converts the specified 64-bit signed integer to a double-precision floating
/// point number.
///
/// Parameters:
/// value:
/// The number to convert.
///
/// Returns:
/// A double-precision floating point number whose value is equivalent to value.
///</summary>
public double Int64BitsToDouble(long value) { throw new NotImplementedException(); }
///
/// <summary>
/// Returns a Boolean value converted from one byte at a specified position in
/// a byte array.
///
/// Parameters:
/// value:
/// An array of bytes.
///
/// startIndex:
/// The starting position within value.
///
/// Returns:
/// true if the byte at startIndex in value is nonzero; otherwise, false.
///
/// Exceptions:
/// System.ArgumentNullException:
/// value is null.
///
/// System.ArgumentOutOfRangeException:
/// startIndex is less than zero or greater than the length of value minus 1.
///</summary>
public bool ToBoolean(byte[] value, int startIndex) { throw new NotImplementedException(); }
///
/// <summary>
/// Returns a Unicode character converted from two bytes at a specified position
/// in a byte array.
///
/// Parameters:
/// value:
/// An array.
///
/// startIndex:
/// The starting position within value.
///
/// Returns:
/// A character formed by two bytes beginning at startIndex.
///
/// Exceptions:
/// System.ArgumentException:
/// startIndex equals the length of value minus 1.
///
/// System.ArgumentNullException:
/// value is null.
///
/// System.ArgumentOutOfRangeException:
/// startIndex is less than zero or greater than the length of value minus 1.
///</summary>
public char ToChar(byte[] value, int startIndex) { throw new NotImplementedException(); }
///
/// <summary>
/// Returns a double-precision floating point number converted from eight bytes
/// at a specified position in a byte array.
///
/// Parameters:
/// value:
/// An array of bytes.
///
/// startIndex:
/// The starting position within value.
///
/// Returns:
/// A double precision floating point number formed by eight bytes beginning
/// at startIndex.
///
/// Exceptions:
/// System.ArgumentException:
/// startIndex is greater than or equal to the length of value minus 7, and is
/// less than or equal to the length of value minus 1.
///
/// System.ArgumentNullException:
/// value is null.
///
/// System.ArgumentOutOfRangeException:
/// startIndex is less than zero or greater than the length of value minus 1.
///</summary>
public double ToDouble(byte[] value, int startIndex)
{
if (IsLittleEndian)
{
return System.BitConverter.ToDouble(value, startIndex);
}
else
{
byte[] res = new byte[8];
Array.Copy(value, startIndex, res, 0, 8);
Array.Reverse(res);
return System.BitConverter.ToDouble(res, 0);
}
}
///
/// <summary>
/// Returns a 16-bit signed integer converted from two bytes at a specified position
/// in a byte array.
///
/// Parameters:
/// value:
/// An array of bytes.
///
/// startIndex:
/// The starting position within value.
///
/// Returns:
/// A 16-bit signed integer formed by two bytes beginning at startIndex.
///
/// Exceptions:
/// System.ArgumentException:
/// startIndex equals the length of value minus 1.
///
/// System.ArgumentNullException:
/// value is null.
///
/// System.ArgumentOutOfRangeException:
/// startIndex is less than zero or greater than the length of value minus 1.
///</summary>
public short ToInt16(byte[] value, int startIndex)
{
if (IsLittleEndian)
{
return System.BitConverter.ToInt16(value, startIndex);
}
else
{
byte[] res = (byte[])value.Clone();
Array.Reverse(res);
return System.BitConverter.ToInt16(res, value.Length - sizeof(Int16) - startIndex);
}
}
///
/// <summary>
/// Returns a 32-bit signed integer converted from four bytes at a specified
/// position in a byte array.
///
/// Parameters:
/// value:
/// An array of bytes.
///
/// startIndex:
/// The starting position within value.
///
/// Returns:
/// A 32-bit signed integer formed by four bytes beginning at startIndex.
///
/// Exceptions:
/// System.ArgumentException:
/// startIndex is greater than or equal to the length of value minus 3, and is
/// less than or equal to the length of value minus 1.
///
/// System.ArgumentNullException:
/// value is null.
///
/// System.ArgumentOutOfRangeException:
/// startIndex is less than zero or greater than the length of value minus 1.
///</summary>
public int ToInt32(byte[] value, int startIndex)
{
if (IsLittleEndian)
{
return System.BitConverter.ToInt32(value, startIndex);
}
else
{
byte[] res = (byte[])value.Clone();
Array.Reverse(res);
return System.BitConverter.ToInt32(res, value.Length - sizeof(Int32) - startIndex);
}
}
///
/// <summary>
/// Returns a 64-bit signed integer converted from eight bytes at a specified
/// position in a byte array.
///
/// Parameters:
/// value:
/// An array of bytes.
///
/// startIndex:
/// The starting position within value.
///
/// Returns:
/// A 64-bit signed integer formed by eight bytes beginning at startIndex.
///
/// Exceptions:
/// System.ArgumentException:
/// startIndex is greater than or equal to the length of value minus 7, and is
/// less than or equal to the length of value minus 1.
///
/// System.ArgumentNullException:
/// value is null.
///
/// System.ArgumentOutOfRangeException:
/// startIndex is less than zero or greater than the length of value minus 1.
///</summary>
public long ToInt64(byte[] value, int startIndex)
{
if (IsLittleEndian)
{
return System.BitConverter.ToInt64(value, startIndex);
}
else
{
byte[] res = (byte[])value.Clone();
Array.Reverse(res);
return System.BitConverter.ToInt64(res, value.Length - sizeof(Int64) - startIndex);
}
}
///
/// <summary>
/// Returns a single-precision floating point number converted from four bytes
/// at a specified position in a byte array.
///
/// Parameters:
/// value:
/// An array of bytes.
///
/// startIndex:
/// The starting position within value.
///
/// Returns:
/// A single-precision floating point number formed by four bytes beginning at
/// startIndex.
///
/// Exceptions:
/// System.ArgumentException:
/// startIndex is greater than or equal to the length of value minus 3, and is
/// less than or equal to the length of value minus 1.
///
/// System.ArgumentNullException:
/// value is null.
///
/// System.ArgumentOutOfRangeException:
/// startIndex is less than zero or greater than the length of value minus 1.
///</summary>
public float ToSingle(byte[] value, int startIndex)
{
if (IsLittleEndian)
{
return System.BitConverter.ToSingle(value, startIndex);
}
else
{
byte[] res = (byte[])value.Clone();
Array.Reverse(res);
return System.BitConverter.ToSingle(res, value.Length - sizeof(Single) - startIndex);
}
}
///
/// <summary>
/// Converts the numeric value of each element of a specified array of bytes
/// to its equivalent hexadecimal string representation.
///
/// Parameters:
/// value:
/// An array of bytes.
///
/// Returns:
/// A System.String of hexadecimal pairs separated by hyphens, where each pair
/// represents the corresponding element in value; for example, "7F-2C-4A".
///
/// Exceptions:
/// System.ArgumentNullException:
/// value is null.
///</summary>
public string ToString(byte[] value)
{
if (IsLittleEndian)
{
return System.BitConverter.ToString(value);
}
else
{
byte[] res = (byte[])value.Clone();
Array.Reverse(res);
return System.BitConverter.ToString(res);
}
}
///
/// <summary>
/// Converts the numeric value of each element of a specified subarray of bytes
/// to its equivalent hexadecimal string representation.
///
/// Parameters:
/// value:
/// An array of bytes.
///
/// startIndex:
/// The starting position within value.
///
/// Returns:
/// A System.String of hexadecimal pairs separated by hyphens, where each pair
/// represents the corresponding element in a subarray of value; for example,
/// "7F-2C-4A".
///
/// Exceptions: