-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.Conversion.pas
1123 lines (961 loc) · 27.8 KB
/
JPL.Conversion.pas
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
unit JPL.Conversion;
{
Jacek Pazera
https://www.pazera-software.com
https://github.com/jackdp
Based on my old unit from 2000 for Borland Pascal 7.0
}
{$I .\..\jp.inc}
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$WARN 5091 off : Local variable "$1" of a managed type does not seem to be initialized}
{$ENDIF}
interface
uses
{$IFDEF DCC} Windows, {$ENDIF}
SysUtils,
JPL.Strings;
type
TTimeUnit = (tuMillisecond, tuSecond, tuMinute, tuHour, tuDay {24h});
function BoolToStr(const b: Boolean; TrueStr: string = 'True'; FalseStr: string = 'False'): string;
function BoolToStrYesNo(const Value: Boolean): string;
function BoolToStrYN(const Value: Boolean): string;
function BoolToStr10(const Value: Boolean): string;
function BoolToStrOnOff(const Value: Boolean): string;
function BoolToStrTF(const Value: Boolean): string;
function StrToBool(const s: string): Boolean;
function BoolToInt(const B: Boolean): integer;
function BoolToByte(const B: Boolean): Byte;
function IntToHexEx(const Value: integer; HexDigits: integer = 2; HexPrefix: string = '$'; bLowerCase: Boolean = True; BytesSeparator: string = '';
TrimLeadingZeros: Boolean = False): string;
function Int64ToHexEx(const Value: Int64; HexDigits: integer = 2; HexPrefix: string = '$'; bLowerCase: Boolean = True; BytesSeparator: string = '';
TrimLeadingZeros: Boolean = False): string;
function UInt64ToHexEx(const Value: UInt64; HexDigits: integer = 2; HexPrefix: string = '$'; bLowerCase: Boolean = True; BytesSeparator: string = '';
TrimLeadingZeros: Boolean = False): string;
function HexToInt(Hex: string; ErrorVal: integer = -1): integer;
function HexToInt64(Hex: string; ErrorVal: integer = -1): int64;
function HexToUInt(Hex: string): UInt32;
function HexToUInt64(Hex: string): UInt64;
function HexToBin(Hex: string): string;
function PowerInt(Base, Exponent: integer): integer;
function PowerInt64(Base, Exponent: Int64): Int64;
function PowerUInt(const Base, Exponent: UInt32): UInt32;
function PowerUInt64(const Base, Exponent: UInt32): UInt64;
function BinToInt(Bin: string): integer;
function TryBinToInt(BinStr: string; var xInt: Int64): Boolean;
function BinToInt64(Bin: string): int64;
function BinToUInt(Bin: string): UInt32;
function BinToUInt64(Bin: string): UInt64;
function BinToHex(Bin: string): string;
function IntToBin(Int: int64): string;
function IntToBinEx(Int: int64; Digits: byte): string;
function StrToDec(const s: string): integer;
function StrToDec64(const s: string): Int64;
function StrToUInt(const NumStr: string): UInt32;
function TryStrToUInt(const NumStr: string; out xResult: UInt32): Boolean;
function StrToUInt64(const NumStr: string): UInt64;
function TryStrToUInt64(const NumStr: string; out xResult: UInt64): Boolean;
function StrToWord(const NumStr: string): Word;
function TryStrToWord(const NumStr: string; out xResult: Word): Boolean;
function StrToDWord(const NumStr: string): DWORD;
function TryStrToDWord(const NumStr: string; out xResult: DWORD): Boolean;
function ByteToHex(B: byte): string;
function ftos(xr: Extended; RoundTo: integer = 2; DSep: string = ''): string;
function ftosEx(xr: Extended; Format: string = '0.00'; DecSeparator: string = '.'; ThousandSeparator: string = ''): string;
function stof(s: string; ErrorValue: Real = -1): Real;
function TryStoF(s: string; var x: Single): Boolean; overload;
function TryStoF(s: string; var x: Double): Boolean; overload;
{$IFDEF FPC}
{$IFNDEF HAS_UINTTOSTR}
function UIntToStr(Value: QWord): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
function UIntToStr(Value: Cardinal): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
{$ENDIF}
{$ENDIF}
function itos(const x: integer): string; overload;
function itos(const x: Int64): string; overload;
function itos(const x: Cardinal): string; overload;
function itos(const x: UInt64): string; overload;
function i64tos(const x: int64): string;
function stoi(const s: string): integer;
function stoi64(const s: string): int64;
function IsValidInteger(const s: string): Boolean;
function IsValidInteger64(const s: string): Boolean;
function IsValidFloat(const s: string): Boolean;
function MSecToTimeStr(xmsec: int64; bShowMSec: Boolean = True): string;
function GetIntInRange(const Value, Min, Max: integer): integer; overload;
function GetIntInRange(const Value, Min, Max: Int64): Int64; overload;
function GetIntInRange(const Value, Min, Max: SmallInt): SmallInt; overload;
function GetIntInRange(const Value, Min, Max: ShortInt): ShortInt; overload;
function GetFloatInRange(const Value, Min, Max: Single): Single; overload;
function GetFloatInRange(const Value, Min, Max: Real): Real; overload;
function GetFloatInRange(const Value, Min, Max: Double): Double; overload;
procedure LimitValue(var Value: integer; const Min, Max: integer); overload;
procedure LimitValue(var Value: Int64; const Min, Max: Int64); overload;
function IsValidBinStr(BinStr: string; IgnoreSpaces: Boolean = False): Boolean;
function IsValidHexStr(HexStr: string; IgnoreSpaces: Boolean = False): Boolean;
function IsValidIntStr(IntStr: string; IgnoreSpaces: Boolean = False): Boolean;
function IsValidFloatStr(FloatStr: string; IgnoreSpaces: Boolean = False): Boolean;
function TryStrToByte(s: string; var bt: Byte): Boolean;
function TryHexToInt(Hex: string; var xInt: Int64): Boolean; overload;
function TryHexToInt(Hex: string; var xInt: integer): Boolean; overload;
function TryHexToByte(Hex: string; var xb: Byte): Boolean;
function TryGetMilliseconds(const NumStr: string; out MilliSeconds: Int64; DefaultTimeUnit: TTimeUnit = tuMillisecond): Boolean;
implementation
uses
JPL.TStr;
function TryGetMilliseconds(const NumStr: string; out MilliSeconds: Int64; DefaultTimeUnit: TTimeUnit = tuMillisecond): Boolean;
var
s: string;
dx64: Double;
ux64: UInt64;
bSeconds, bMinutes, bHours, bDays: Boolean;
begin
Result := False;
s := TStr.TrimAndLow(NumStr);
s := TStr.TrimBounds(s, '"', '"');
s := TStr.RemoveSpaces(s);
bSeconds := False;
bMinutes := False;
bHours := False;
bDays := False;
if TStr.EndsStr('ms', s) then s := TStr.TrimFromEnd(s, 'ms')
else if TStr.EndsStr('s', s) then
begin
bSeconds := True;
s := TStr.TrimFromEnd(s, 's');
end
else if TStr.EndsStr('m', s) then
begin
bMinutes := True;
s := TStr.TrimFromEnd(s, 'm');
end
else if TStr.EndsStr('h', s) then
begin
bHours := True;
s := TStr.TrimFromEnd(s, 'h');
end
else if TStr.EndsStr('d', s) then
begin
bDays := True;
s := TStr.TrimFromEnd(s, 'd');
end
else
begin
// No time unit specified. Using DefaultTimeUnit
case DefaultTimeUnit of
tuSecond: bSeconds := True;
tuMinute: bMinutes := True;
tuHour: bHours := True;
tuDay: bDays := True;
end;
end;
dx64 := 0;
if (TStr.StartsWithHexPrefix(s)) or (Copy(s, 1, 1) = '%') then
begin
if not TryStrToUInt64(s, ux64) then Exit;
dx64 := ux64;
end
else if not TryStoF(s, dx64) then Exit;
if bSeconds then dx64 := dx64 * 1000
else if bMinutes then dx64 := dx64 * 1000 * 60
else if bHours then dx64 := dx64 * 1000 * 60 * 60
else if bDays then dx64 := dx64 * 1000 * 60 * 60 * 24;
MilliSeconds := Int64(Round(dx64));
Result := True;
end;
function TryHexToByte(Hex: string; var xb: Byte): Boolean;
var
x: Int64;
begin
Result := False;
x := 0;
if not TryHexToInt(Hex, x) then Exit;
if (x < 0) or (x > 255) then Exit;
Result := True;
xb := Byte(x);
end;
function TryHexToInt(Hex: string; var xInt: integer): Boolean; overload;
var
Code: integer;
x: integer;
begin
Result := True;
if Hex = '' then Exit(False);
if Hex[1] <> '$' then Hex := '$' + Hex;
Val(Hex, x, Code);
if Code = 0 then xInt := x
else Result := False;
end;
function TryHexToInt(Hex: string; var xInt: Int64): Boolean;
var
Code: integer;
i64: int64;
begin
Result := True;
if Hex = '' then Exit(False);
if Hex[1] <> '$' then Hex := '$' + Hex;
Val(Hex, i64, Code);
if Code = 0 then xInt := i64
else Result := False;
end;
function TryStrToByte(s: string; var bt: Byte): Boolean;
var
x: integer;
begin
Result := False;
if not TryStrToInt(s, x) then Exit;
if not (x in [0..255]) then Exit;
bt := Byte(x);
Result := True;
end;
function GetFloatInRange(const Value, Min, Max: Double): Double;
begin
if Value < Min then Result := Min
else if Value > Max then Result := Max
else Result := Value;
end;
function GetFloatInRange(const Value, Min, Max: Real): Real;
begin
if Value < Min then Result := Min
else if Value > Max then Result := Max
else Result := Value;
end;
function GetFloatInRange(const Value, Min, Max: Single): Single;
begin
if Value < Min then Result := Min
else if Value > Max then Result := Max
else Result := Value;
end;
function GetIntInRange(const Value, Min, Max: integer): integer;
begin
if Value < Min then Result := Min
else if Value > Max then Result := Max
else Result := Value;
end;
function GetIntInRange(const Value, Min, Max: Int64): Int64;
begin
if Value < Min then Result := Min
else if Value > Max then Result := Max
else Result := Value;
end;
function GetIntInRange(const Value, Min, Max: SmallInt): SmallInt;
begin
if Value < Min then Result := Min
else if Value > Max then Result := Max
else Result := Value;
end;
function GetIntInRange(const Value, Min, Max: ShortInt): ShortInt;
begin
if Value < Min then Result := Min
else if Value > Max then Result := Max
else Result := Value;
end;
procedure LimitValue(var Value: integer; const Min, Max: integer);
begin
if Value < Min then Value := Min
else if Value > Max then Value := Max;
end;
procedure LimitValue(var Value: Int64; const Min, Max: Int64);
begin
if Value < Min then Value := Min
else if Value > Max then Value := Max;
end;
function IsValidFloatStr(FloatStr: string; IgnoreSpaces: Boolean = False): Boolean;
var
i, xCount, xDots, xCommas: integer;
begin
Result := False;
if IgnoreSpaces then FloatStr := StrRemove(FloatStr, ' ');
xCount := CharCount('-', FloatStr);
if xCount > 1 then Exit;
if (xCount = 1) and (FloatStr[1] <> '-') then Exit;
if Copy(FloatStr, 1, 1) = '+' then Delete(FloatStr, 1, 1);
FloatStr := UpperCase(FloatStr);
if CharCount('E', FloatStr) > 1 then Exit;
xDots := CharCount('.', FloatStr);
xCommas := CharCount(',', FloatStr);
if xDots > 1 then Exit;
if xCommas > 1 then Exit;
if xDots + xCommas = 2 then Exit;
Result := True;
for i := 1 to Length(FloatStr) do
if not (CharInSet(FloatStr[i], ['0'..'9']) or (FloatStr[i] = '-') or (FloatStr[i] = 'E') or (FloatStr[i] = '.') or (FloatStr[i] = ',')) then
begin
Result := False;
Break;
end;
end;
function IsValidIntStr(IntStr: string; IgnoreSpaces: Boolean = False): Boolean;
var
i, xm: integer;
begin
Result := False;
if IgnoreSpaces then IntStr := StrRemove(IntStr, ' ');
if IntStr = '' then Exit;
xm := CharCount('-', IntStr);
if xm > 1 then Exit;
if (xm = 1) and (IntStr[1] <> '-') then Exit;
if Copy(IntStr, 1, 1) = '+' then Delete(IntStr, 1, 1);
Result := True;
for i := 1 to Length(IntStr) do
if not (
CharInSet(IntStr[i], ['0'..'9']) or (IntStr[i] = '-')
) then
begin
Result := False;
Break;
end;
end;
function IsValidHexStr(HexStr: string; IgnoreSpaces: Boolean = False): Boolean;
var
i: integer;
begin
Result := True;
if IgnoreSpaces then HexStr := StrRemove(HexStr, ' ');
HexStr := UpperCase(HexStr);
for i := 1 to Length(HexStr) do
if not (
CharInSet(HexStr[i], ['0'..'9']) or CharInSet(HexStr[i], ['A'..'F']) // or (HexStr[i] = '-')
) then
begin
Result := False;
Break;
end;
end;
function IsValidBinStr(BinStr: string; IgnoreSpaces: Boolean = False): Boolean;
var
i: integer;
begin
Result := True;
if IgnoreSpaces then BinStr := StrRemove(BinStr, ' ');
for i := 1 to Length(BinStr) do
if not CharInSet(BinStr[i], ['0'..'1']) then
begin
Result := False;
Break;
end;
end;
{$IFDEF FPC}
{$IFNDEF HAS_UINTTOSTR}
function UIntToStr(Value: QWord): string;
begin
Result := IntTostr(Value);
end;
function UIntToStr(Value: Cardinal): string;
begin
System.Str(Value, Result);
end;
{$ENDIF}
{$ENDIF}
function itos(const x: integer): string;
begin
Result := IntToStr(x);
end;
function itos(const x: Int64): string;
begin
Result := IntToStr(x);
end;
function itos(const x: Cardinal): string;
begin
Result := UIntToStr(x);
end;
function itos(const x: UInt64): string;
begin
Result := UIntToStr(x);
end;
function i64tos(const x: int64): string;
begin
Result := IntToStr(x);
end;
function stoi(const s: string): integer;
begin
Result := StrToInt(s);
end;
function stoi64(const s: string): int64;
begin
Result := StrToInt64(s);
end;
function MSecToTimeStr(xmsec: int64; bShowMSec: Boolean = True): string;
var
Days, H, M, Sec, MSec: DWORD;
sr: string;
begin
Days := xmsec div 86400000;
xmsec := xmsec - (int64(Days) * 86400000);
H := xmsec div 3600000;
xmsec := xmsec - (int64(H) * 3600000);
M := xmsec div 60000;
xmsec := xmsec - (int64(M) * 60000);
Sec := xmsec div 1000;
xmsec := xmsec - (int64(Sec) * 1000);
MSec := xmsec;
sr :=
Pad(IntToStr(H), 2, '0') + ':' +
Pad(IntToStr(M), 2, '0') + ':' +
Pad(IntToStr(Sec), 2, '0');
if bShowMSec and (MSec > 0) then sr := sr + '.' + Pad(IntToStr(MSec), 3, '0');
if Days > 0 then sr := IntToStr(Days) + 'd ' + sr;
Result := sr;
end;
function IsValidInteger(const s: string): Boolean;
begin
try
StrToInt(s);
Result := True;
except
on EConvertError do Result := False;
end;
end;
function IsValidInteger64(const s: string): Boolean;
begin
try
StrToInt64(s);
Result := True;
except
on EConvertError do Result := False;
end;
end;
function IsValidFloat(const s: string): Boolean;
begin
try
StrToFloat(s);
Result := True;
except
on EConvertError do Result := False;
end;
end;
function TryStoF(s: string; var x: Double): Boolean; overload;
begin
s := StringReplace(s, ',', GetDecimalSeparator{%H-}, [rfReplaceAll]);
s := StringReplace(s, '.', GetDecimalSeparator{%H-}, [rfReplaceAll]);
Result := TryStrToFloat(s, x);
end;
function TryStoF(s: string; var x: Single): Boolean; overload;
begin
s := StringReplace(s, ',', GetDecimalSeparator{%H-}, [rfReplaceAll]);
s := StringReplace(s, '.', GetDecimalSeparator{%H-}, [rfReplaceAll]);
Result := TryStrToFloat(s, x);
end;
function stof(s: string; ErrorValue: Real): Real;
begin
s := StringReplace(s, ',', GetDecimalSeparator, [rfReplaceAll]);
s := StringReplace(s, '.', GetDecimalSeparator, [rfReplaceAll]);
try
Result := StrToFloat(s);
except
Result := ErrorValue;
end;
end;
function ftos(xr: Extended; RoundTo: integer = 2; DSep: string = ''): string;
var
s, FormatMask: string;
i: integer;
begin
if Round(xr) = xr then
s := IntToStr(Round(xr))
else
begin
if RoundTo <= 0 then FormatMask := ''
else FormatMask := '0.' + StringOfChar('0', RoundTo);
s := FormatFloat(FormatMask, xr);
for i := Length(s) downto 1 do
begin
if s[i] <> '0' then Break;
Delete(s, i, 1);
end;
end;
if s[Length(s)] = GetDecimalSeparator then Delete(s, Length(s), 1);
if DSep <> '' then s := StringReplace(s, GetDecimalSeparator, DSep, []);
Result := s;
end;
function ftosEx(xr: Extended; Format: string = '0.00'; DecSeparator: string = '.'; ThousandSeparator: string = ''): string;
var
xp, i, Len, k: integer;
begin
Result := FormatFloat(Format, xr);
Result := StringReplace(Result, GetDecimalSeparator, DecSeparator, []);
xp := Pos(DecSeparator, Result);
if xp = 0 then xp := Length(Result);
k := (xp - 1) div 3;
Len := xp - 1;
for i := 1 to k do Insert(ThousandSeparator, Result, Len - (i * 3) + 1);
if Copy(Result, 1, Length(ThousandSeparator)) = ThousandSeparator then Delete(Result, 1, Length(ThousandSeparator));
end;
function ByteToHex(B: byte): string;
const
hex: array[0..15] of Char = '0123456789ABCDEF';
begin
Result := hex[B shr 4] + hex[B and 15];
end;
function BinToHex(Bin: string): string;
var
int: integer;
begin
int := BinToInt(Bin);
Result := IntToHex(int, 0);
end;
function HexToBin(Hex: string): string;
var
int: integer;
begin
int := HexToInt(Hex);
Result := IntToBin(int);
end;
function IntToBin(Int: int64): string;
var
x, y: int64;
begin
Result := '';
x := Int;
repeat
y := x mod 2;
x := x div 2;
if y > 0 then
Result := '1' + Result
else
Result := '0' + Result;
until x = 0;
end;
function IntToBinEx(Int: int64; Digits: byte): string;
var
bin: string;
i: integer;
begin
bin := IntToBin(Int);
i := Digits - length(bin);
if i > 0 then
for i := 1 to (Digits - length(bin)) do
bin := '0' + bin;
Result := bin;
end;
function BinToInt(Bin: string): integer;
var
LengthBin, deleted, i, x: integer;
arr: array of integer; // = nil;
begin
SetLength(arr, 0);
Result := 0;
if pos('1', Bin) > 0 then
begin
deleted := 0;
LengthBin := length(Bin);
repeat
x := pos('1', Bin);
deleted := deleted + x;
SetLength(arr, length(arr) + 1);
arr[length(arr) - 1] := LengthBin - deleted;
Bin := copy(Bin, x + 1, length(Bin));
until (pos('1', Bin) = 0);
for i := 0 to length(arr) - 1 do
Result := Result + PowerInt(2, arr[i]);
end;
end;
function TryBinToInt(BinStr: string; var xInt: Int64): Boolean;
var
LengthBin, Deleted, i, x: integer;
arr: array of Int64;
begin
Result := True;
try
SetLength(arr, 0);
xInt := 0;
if Pos('1', BinStr) > 0 then
begin
Deleted := 0;
LengthBin := Length(BinStr);
repeat
x := Pos('1', BinStr);
Deleted := Deleted + x;
SetLength(arr, Length(arr) + 1);
arr[Length(arr) - 1] := Int64(LengthBin) - Int64(Deleted);
BinStr := Copy(BinStr, x + 1, Length(BinStr));
until (Pos('1', BinStr) = 0);
for i := 0 to Length(arr) - 1 do
xInt := xInt + PowerInt64(2, arr[i]);
if xInt < 0 then Result := False;
end;
except
Result := False;
end;
end;
function BinToInt64(Bin: string): int64;
var
LengthBin, deleted, i, x: integer;
arr: array of integer; // = nil;
begin
SetLength(arr, 0);
Result := 0;
if pos('1', Bin) > 0 then
begin
deleted := 0;
LengthBin := length(Bin);
repeat
x := pos('1', Bin);
deleted := deleted + x;
SetLength(arr, length(arr) + 1);
arr[length(arr) - 1] := LengthBin - deleted;
Bin := copy(Bin, x + 1, length(Bin));
until (pos('1', Bin) = 0);
for i := 0 to length(arr) - 1 do
Result := Result + PowerInt(2, arr[i]);
end;
end;
function BinToUInt(Bin: string): UInt32;
var
LengthBin, deleted, i, x: integer;
arr: array of integer;
begin
SetLength(arr, 0);
Result := 0;
if pos('1', Bin) > 0 then
begin
deleted := 0;
LengthBin := length(Bin);
repeat
x := pos('1', Bin);
deleted := deleted + x;
SetLength(arr, length(arr) + 1);
arr[length(arr) - 1] := LengthBin - deleted;
Bin := copy(Bin, x + 1, length(Bin));
until (pos('1', Bin) = 0);
for i := 0 to length(arr) - 1 do
Result := Result + PowerUInt(2, arr[i]);
end;
end;
function BinToUInt64(Bin: string): UInt64;
var
LengthBin, deleted, i, x: integer;
arr: array of integer;
begin
SetLength(arr, 0);
Result := 0;
if pos('1', Bin) > 0 then
begin
deleted := 0;
LengthBin := length(Bin);
repeat
x := pos('1', Bin);
deleted := deleted + x;
SetLength(arr, length(arr) + 1);
arr[length(arr) - 1] := LengthBin - deleted;
Bin := copy(Bin, x + 1, length(Bin));
until (pos('1', Bin) = 0);
for i := 0 to length(arr) - 1 do
Result := Result + PowerUInt64(2, arr[i]);
end;
end;
function PowerInt(Base, Exponent: integer): integer;
var
x, i: integer;
begin
if Exponent = 0 then Result := 1
else if Exponent = 1 then Result := Base
else
begin
x := Base;
for i := 1 to Exponent - 1 do Base := Base * x;
Result := Base;
end;
end;
function PowerInt64(Base, Exponent: Int64): Int64;
var
x, i: integer;
begin
if Exponent = 0 then Result := 1
else if Exponent = 1 then Result := Base
else
begin
x := Base;
for i := 1 to Exponent - 1 do Base := Base * x;
Result := Base;
end;
end;
function PowerUInt(const Base, Exponent: UInt32): UInt32;
var
i: integer;
x, xb: UInt32;
begin
if Exponent = 0 then Result := 1
else if Exponent = 1 then Result := Base
else
begin
x := Base;
xb := Base;
for i := 1 to Exponent - 1 do xb := xb * x;
Result := xb;
end;
end;
function PowerUInt64(const Base, Exponent: UInt32): UInt64;
var
i: integer;
x, xb: UInt64;
begin
if Exponent = 0 then Result := 1
else if Exponent = 1 then Result := Base
else
begin
x := Base;
xb := Base;
for i := 1 to Exponent - 1 do xb := xb * x;
Result := xb;
end;
end;
function IntToHexEx(const Value: integer; HexDigits: integer = 2; HexPrefix: string = '$'; bLowerCase: Boolean = True; BytesSeparator: string = '';
TrimLeadingZeros: Boolean = False): string;
var
s: string;
begin
s := IntToHex(Value, HexDigits);
if bLowerCase then s := LowerCase(s);
if TrimLeadingZeros then
begin
while True do
begin
if s = '' then Break;
if s[1] <> '0' then Break;
Delete(s, 1, 1);
end;
if Odd(Length(s)) then s := '0' + s;
if s = '' then s := '00';
end
else if Odd(Length(s)) then s := '0' + s;
if BytesSeparator <> '' then s := InsertNumSep(s, BytesSeparator, 2);
s := HexPrefix + s;
Result := s;
end;
function Int64ToHexEx(const Value: Int64; HexDigits: integer = 2; HexPrefix: string = '$'; bLowerCase: Boolean = True; BytesSeparator: string = '';
TrimLeadingZeros: Boolean = False): string;
var
s: string;
begin
s := IntToHex(Value, HexDigits);
if bLowerCase then s := LowerCase(s);
if TrimLeadingZeros then
begin
while True do
begin
if s = '' then Break;
if s[1] <> '0' then Break;
Delete(s, 1, 1);
end;
if Odd(Length(s)) then s := '0' + s;
if s = '' then s := '00';
end
else if Odd(Length(s)) then s := '0' + s;
if BytesSeparator <> '' then s := InsertNumSep(s, BytesSeparator, 2);
s := HexPrefix + s;
Result := s;
end;
function UInt64ToHexEx(const Value: UInt64; HexDigits: integer = 2; HexPrefix: string = '$'; bLowerCase: Boolean = True; BytesSeparator: string = '';
TrimLeadingZeros: Boolean = False): string;
var
s: string;
begin
s := IntToHex(Value, HexDigits);
if bLowerCase then s := LowerCase(s);
if TrimLeadingZeros then
begin
while True do
begin
if s = '' then Break;
if s[1] <> '0' then Break;
Delete(s, 1, 1);
end;
if Odd(Length(s)) then s := '0' + s;
if s = '' then s := '00';
end;
if BytesSeparator <> '' then s := InsertNumSep(s, BytesSeparator, 2);
s := HexPrefix + s;
Result := s;
end;
{$hints off}
function HexToInt(Hex: string; ErrorVal: integer): integer;
var
code, int: integer;
begin
Hex := StringReplace(Hex, '0x', '$', [rfIgnoreCase]);
if Hex[1] <> '$' then Hex := '$' + Hex;
Val(Hex, int, code);
if code = 0 then Result := int
else Result := ErrorVal;
end;
{$hints on}
{$hints off}
function HexToInt64(Hex: string; ErrorVal: integer): int64;
var
code: integer;
i64: int64;
begin
Hex := StringReplace(Hex, '0x', '$', [rfIgnoreCase]);
if Hex[1] <> '$' then Hex := '$' + Hex;
Val(Hex, i64, code);
if code = 0 then Result := i64
else Result := ErrorVal;
end;
{$hints on}
function HexToUInt(Hex: string): UInt32;
var
code: integer;
begin
Hex := StringReplace(Hex, '0x', '$', [rfIgnoreCase]);
if Hex[1] <> '$' then Hex := '$' + Hex;
Val(Hex, Result, code);
if code <> 0 then raise EConvertError.Create('HexToUInt: Cannot convert "' + Hex + '" to UInt32');
end;
function HexToUInt64(Hex: string): UInt64;
var
code: integer;
begin
Hex := StringReplace(Hex, '0x', '$', [rfIgnoreCase]);
if Hex[1] <> '$' then Hex := '$' + Hex;
Val(Hex, Result, code);
if code <> 0 then raise EConvertError.Create('HexToUInt64: Cannot convert "' + Hex + '" to UInt64');
end;
function StrToDec(const s: string): integer;
var
s2, sOne, sTwo: string;
begin
s2 := UpperCase(RemoveSpaces(s));
sOne := Copy(s2, 1, 1);
sTwo := Copy(s2, 1, 2);
if sOne = '$' then Result := HexToInt(s2)
else if sTwo = '0X' then Result := HexToInt('$' + Copy(s2, 3, Length(s2)))
else if sTwo = '-%' then raise EConvertError.Create('StrToDec: Only positive binary numbers are supported!')
else if sOne = '%' then Result := BinToInt(s2)
else Result := StrToInt(s2);
end;
function StrToDec64(const s: string): Int64;
var
s2, sOne, sTwo: string;
begin
s2 := UpperCase(RemoveSpaces(s));
sOne := Copy(s2, 1, 1);
sTwo := Copy(s2, 1, 2);
if sOne = '$' then Result := HexToInt64(s2)
else if sTwo = '0X' then Result := HexToInt64('$' + Copy(s2, 3, Length(s2)))
else if sTwo = '-%' then raise EConvertError.Create('StrToDec64: Only positive binary numbers are supported!')
else if sOne = '%' then Result := BinToInt64(s2)
else Result := StrToInt64(s2);
end;
function StrToUInt(const NumStr: string): UInt32;
var
s2: string;
code: integer;
begin
s2 := RemoveSpaces(NumStr);
if Copy(s2, 1, 1) = '$' then Result := HexToUInt(s2)
else if Copy(s2, 1, 1) = '%' then Result := BinToUInt(s2)
else if UpperCase(Copy(s2, 1, 2)) = '0X' then Result := HexToUInt(s2)
else
begin
Val(s2, Result, code);
if code <> 0 then raise EConvertError.Create('StrToUInt: Cannot convert "' + NumStr + '" to UInt');
end;
end;
function TryStrToUInt(const NumStr: string; out xResult: UInt32): Boolean;