forked from VE3NEA/OmniRig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ByteFuns.pas
161 lines (126 loc) · 3.72 KB
/
ByteFuns.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
//------------------------------------------------------------------------------
//This Source Code Form is subject to the terms of the Mozilla Public
//License, v. 2.0. If a copy of the MPL was not distributed with this
//file, You can obtain one at http://mozilla.org/MPL/2.0/.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Omni-Rig
//
// Copyright (c) 2003 Alex Shovkoplyas, VE3NEA
//
//------------------------------------------------------------------------------
unit ByteFuns;
interface
uses
SysUtils, Math, Windows, Variants;
type
TByteArray = array of Byte;
function BytesAnd(Arr1, Arr2: TByteArray): TByteArray;
function BytesEqual(Arr1, Arr2: TByteArray): boolean;
procedure BytesReverse(Arr: TByteArray);
function BytesToStr(Bytes: TByteArray): AnsiString;
function StrToBytes(S: AnsiString): TByteArray;
function BytesToSafeArray(Bytes: TByteArray): Variant;
function SafeArrayToBytes(Arr: Variant): TByteArray;
function BytesToHex(Bytes: TByteArray): AnsiString;
function StrToHex(S: AnsiString): AnsiString;
implementation
//------------------------------------------------------------------------------
// byte array functions
//------------------------------------------------------------------------------
function BytesEqual(Arr1, Arr2: TByteArray): boolean;
var
i: integer;
begin
Result := false;
if Length(Arr2) <> Length(Arr1) then Exit;
for i:=0 to High(Arr1) do if Arr2[i] <> Arr1[i] then Exit;
Result := true;
end;
//the length of Arr1 and Arr2 must be verified
//before this function is called
function BytesAnd(Arr1, Arr2: TByteArray): TByteArray;
var
i: integer;
begin
SetLength(Result, Min(Length(Arr1), Length(Arr2)));
for i:=0 to High(Arr1) do Result[i] := Arr1[i] and Arr2[i];
end;
procedure BytesReverse(Arr: TByteArray);
var
B: Byte;
i: integer;
begin
if Length(Arr) < 2 then Exit;
for i:=0 to (Length(Arr) div 2)-1 do
begin
B := Arr[i];
Arr[i] := Arr[High(Arr)-i];
Arr[High(Arr)-i] := B;
end;
end;
function BytesToStr(Bytes: TByteArray): AnsiString;
var
Len: integer;
begin
Len := Length(Bytes);
SetLength(Result, Len);
if Len > 0 then Move(Bytes[0], Result[1], Len);
end;
function StrToBytes(S: AnsiString): TByteArray;
var
Len: integer;
begin
Len := Length(S);
SetLength(Result, Len);
if Len > 0 then Move(S[1], Result[0], Len);
end;
function BytesToSafeArray(Bytes: TByteArray): Variant;
var
P: PByte;
begin
if Bytes = nil then
begin
Result := Unassigned;
Exit;
end;
Result := VarArrayCreate([0, High(Bytes)], varByte);
P := VarArrayLock(Result);
try
Move(Bytes[0], P^, Length(Bytes));
finally
VarArrayUnlock(Result);
end;
end;
function SafeArrayToBytes(Arr: Variant): TByteArray;
var
P: PByte;
Len: integer;
begin
Result := nil;
if VarArrayDimCount(Arr) <> 1 then Exit;
if VarArrayLowBound(Arr, 1) <> 0 then Exit;
Len := VarArrayHighBound(Arr, 1) + 1;
SetLength(Result, Len);
if Result = nil then Exit;
P := VarArrayLock(Arr);
try
Move(P^, Result[0], Len);
finally
VarArrayUnlock(Arr);
end;
end;
function BytesToHex(Bytes: TByteArray): AnsiString;
var
i: integer;
begin
Result := '';
if Bytes = nil then Exit;
for i:=0 to High(Bytes) do Result := Result + AnsiString(IntToHex(Bytes[i], 2));
end;
function StrToHex(S: AnsiString): AnsiString;
begin
Result := BytesToHex(StrToBytes(S));
end;
end.