1 |
unit Functions; |
2 |
|
3 |
interface |
4 |
|
5 |
uses TypeDefs, Classes; |
6 |
|
7 |
function BoolToStr(bool: Boolean): String; |
8 |
function HexToLong(hex: String): LongWord; |
9 |
function Decode_Int(buffer: TByteData): LongWord; |
10 |
function Encode_Int(input: LongWord): TByteData; |
11 |
function Decode_Float(buffer: TByteData): Single; |
12 |
function Encode_Float(input: Single): TByteData; |
13 |
function IntToBin(Value: Byte): String; |
14 |
function DataToBin(Data: TByteData): String; |
15 |
function BinToInt(bin: String): Byte; |
16 |
|
17 |
function StringSmaller(string1, string2: String): Boolean; |
18 |
|
19 |
function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String; |
20 |
function FormatFileSize(size: LongWord): String; |
21 |
function CreateHexString(Data: TByteData; HexOnly: Boolean): String; |
22 |
function DecodeHexString(hex: String): TByteData; |
23 |
function GetWinFileName(Name: String): String; |
24 |
//function GetExtractPath: String; |
25 |
|
26 |
function Explode(_string: String; delimiter: Char): TStrings; |
27 |
|
28 |
|
29 |
implementation |
30 |
|
31 |
uses SysUtils, Math, StrUtils; |
32 |
|
33 |
type |
34 |
TValueSwitcher = record |
35 |
case IsFloat: Boolean of |
36 |
True: (ValueFloat: Single); |
37 |
False: (ValueInt: LongWord); |
38 |
end; |
39 |
|
40 |
|
41 |
|
42 |
function BoolToStr(bool: Boolean): String; |
43 |
begin |
44 |
if bool then |
45 |
Result := 'true' |
46 |
else |
47 |
Result := 'false'; |
48 |
end; |
49 |
|
50 |
|
51 |
function HexToLong(hex: String): LongWord; |
52 |
|
53 |
function NormalizeHexString(var hex: String): Boolean; |
54 |
var |
55 |
i: Byte; |
56 |
begin |
57 |
Result := True; |
58 |
if hex[1] = '$' then |
59 |
begin |
60 |
for i := 1 to Length(hex) - 1 do |
61 |
hex[i] := hex[i + 1]; |
62 |
SetLength(hex, Length(hex) - 1); |
63 |
end; |
64 |
if (hex[1] = '0') and (UpCase(hex[2]) = 'X') then |
65 |
begin |
66 |
for i := 1 to Length(hex) - 2 do |
67 |
hex[i] := hex[i + 2]; |
68 |
SetLength(hex, Length(hex) - 2); |
69 |
end; |
70 |
if Length(hex) = 0 then |
71 |
Result := False; |
72 |
end; |
73 |
|
74 |
begin |
75 |
if NormalizeHexString(hex) then |
76 |
Result := StrToInt(hex) |
77 |
else |
78 |
Result := 0; |
79 |
end; |
80 |
|
81 |
|
82 |
function Decode_Int(buffer: TByteData): LongWord; |
83 |
begin |
84 |
Result := buffer[0] + buffer[1] * 256 + buffer[2] * 256 * 256 + buffer[3] * 256 * 256 * 256; |
85 |
end; |
86 |
|
87 |
|
88 |
function Encode_Int(input: LongWord): TByteData; |
89 |
begin |
90 |
SetLength(Result, 4); |
91 |
Result[0] := input mod 256; |
92 |
input := input div 256; |
93 |
Result[1] := input mod 256; |
94 |
input := input div 256; |
95 |
Result[2] := input mod 256; |
96 |
input := input div 256; |
97 |
Result[3] := input mod 256; |
98 |
end; |
99 |
|
100 |
|
101 |
function Decode_Float(buffer: TByteData): Single; |
102 |
var |
103 |
_valueswitcher: TValueSwitcher; |
104 |
begin |
105 |
_valueswitcher.ValueInt := Decode_Int(buffer); |
106 |
Result := _valueswitcher.ValueFloat; |
107 |
if IsNAN(Result) then |
108 |
Result := 0.0; |
109 |
end; |
110 |
|
111 |
|
112 |
function Encode_Float(input: Single): TByteData; |
113 |
var |
114 |
_valueswitcher: TValueSwitcher; |
115 |
begin |
116 |
_valueswitcher.ValueFloat := input; |
117 |
Result := Encode_Int(_valueswitcher.ValueInt); |
118 |
end; |
119 |
|
120 |
|
121 |
function IntToBin(Value: Byte): String; |
122 |
var |
123 |
i: Byte; |
124 |
begin |
125 |
Result := ''; |
126 |
for i := 7 downto 0 do |
127 |
Result := Result + IntToStr((Value shr i) and $01); |
128 |
end; |
129 |
|
130 |
|
131 |
function DataToBin(Data: TByteData): String; |
132 |
var |
133 |
i, j: Byte; |
134 |
singlebyte: Byte; |
135 |
bytepart: String; |
136 |
begin |
137 |
SetLength(bytepart, 8); |
138 |
Result := ''; |
139 |
for i := 0 to High(Data) do |
140 |
begin |
141 |
singlebyte := Data[i]; |
142 |
for j := 7 downto 0 do |
143 |
begin |
144 |
bytepart[j + 1] := Char((singlebyte and $01) + 48); |
145 |
singlebyte := singlebyte shr 1; |
146 |
end; |
147 |
Result := Result + bytepart + ' '; |
148 |
end; |
149 |
end; |
150 |
|
151 |
|
152 |
function BinToInt(bin: String): Byte; |
153 |
var |
154 |
Add: Integer; |
155 |
i: Byte; |
156 |
begin |
157 |
Result := 0; |
158 |
if Length(bin) <> 8 then |
159 |
Exit; |
160 |
Add := 1; |
161 |
for i := 8 downto 1 do |
162 |
begin |
163 |
if not (bin[i] in ['0', '1']) then |
164 |
Exit; |
165 |
if bin[i] = '1' then |
166 |
Inc(Result, Add); |
167 |
Add := Add shl 1; |
168 |
end; |
169 |
end; |
170 |
|
171 |
|
172 |
|
173 |
function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String; |
174 |
begin |
175 |
Result := AnsiReplaceStr(Format('%' + IntToStr(Width) + 'u', [Value]), ' ', leadingzeros); |
176 |
end; |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
function FormatFileSize(size: LongWord): String; |
182 |
var |
183 |
floatformat: TFormatSettings; |
184 |
begin |
185 |
floatformat.DecimalSeparator := '.'; |
186 |
if size >= 1000 * 1024 * 1024 then |
187 |
Result := FloatToStrF(size / 1024 / 1024 / 1024, ffFixed, 5, 1, floatformat) + ' GB' |
188 |
else |
189 |
if size >= 1000 * 1024 then |
190 |
Result := FloatToStrF(size / 1024 / 1024, ffFixed, 5, 1, floatformat) + ' MB' |
191 |
else |
192 |
if size >= 1000 then |
193 |
Result := FloatToStrF(size / 1024, ffFixed, 5, 1, floatformat) + ' KB' |
194 |
else |
195 |
Result := IntToStr(size) + ' B'; |
196 |
end; |
197 |
|
198 |
|
199 |
|
200 |
|
201 |
function CreateHexString(Data: TByteData; HexOnly: Boolean): String; |
202 |
var |
203 |
string_build, ascii_version: String; |
204 |
i: Integer; |
205 |
begin |
206 |
string_build := ''; |
207 |
ascii_version := ''; |
208 |
for i := 0 to High(Data) do |
209 |
begin |
210 |
if not HexOnly then |
211 |
if (i mod 16) = 0 then |
212 |
string_build := string_build + '0x' + IntToHex(i, 6) + ' '; |
213 |
string_build := string_build + IntToHex(Data[i], 2); |
214 |
if not HexOnly then |
215 |
begin |
216 |
if Data[i] >= 32 then |
217 |
ascii_version := ascii_version + Chr(Data[i]) |
218 |
else |
219 |
ascii_version := ascii_version + '.'; |
220 |
if ((i + 1) mod 2) = 0 then |
221 |
string_build := string_build + #32; |
222 |
if ((i + 1) mod 16) = 0 then |
223 |
begin |
224 |
string_build := string_build + #32 + ascii_version + CrLf; |
225 |
ascii_version := ''; |
226 |
end; |
227 |
end; |
228 |
end; |
229 |
Result := string_build; |
230 |
end; |
231 |
|
232 |
|
233 |
|
234 |
|
235 |
function DecodeHexString(hex: String): TByteData; |
236 |
var |
237 |
i: Integer; |
238 |
begin |
239 |
SetLength(Result, Length(hex) div 2); |
240 |
for i := 0 to Length(Result) do |
241 |
begin |
242 |
Result[i] := 0; |
243 |
case UpCase(hex[1 + i * 2]) of |
244 |
'0'..'9': |
245 |
Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 48) * 16; |
246 |
'A'..'F': |
247 |
Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 55) * 16; |
248 |
end; |
249 |
case UpCase(hex[1 + i * 2 + 1]) of |
250 |
'0'..'9': |
251 |
Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 48); |
252 |
'A'..'F': |
253 |
Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 55); |
254 |
end; |
255 |
end; |
256 |
end; |
257 |
|
258 |
|
259 |
|
260 |
|
261 |
function StringSmaller(string1, string2: String): Boolean; |
262 |
var |
263 |
i: Integer; |
264 |
len: Integer; |
265 |
begin |
266 |
len := Min(Length(string1), Length(string2)); |
267 |
for i := 1 to len do |
268 |
if Ord(string1[i]) <> Ord(string2[i]) then |
269 |
begin |
270 |
Result := Ord(string1[i]) < Ord(string2[i]); |
271 |
Exit; |
272 |
end; |
273 |
Result := Length(string1) < Length(string2); |
274 |
end; |
275 |
|
276 |
|
277 |
|
278 |
function Explode(_string: String; delimiter: Char): TStrings; |
279 |
var |
280 |
start, len: Word; |
281 |
begin |
282 |
Result := TStringList.Create; |
283 |
start := 1; |
284 |
while PosEx(delimiter, _string, start) > 0 do |
285 |
begin |
286 |
len := PosEx(delimiter, _string, start) - start; |
287 |
Result.Add(MidStr(_string, start, len)); |
288 |
start := start + len + 1; |
289 |
end; |
290 |
Result.Add(MidStr(_string, start, Length(_string) - start + 1)); |
291 |
end; |
292 |
|
293 |
|
294 |
function GetWinFileName(Name: String): String; |
295 |
begin |
296 |
Result := Name; |
297 |
Result := AnsiReplaceStr(Result, '\', '__'); |
298 |
Result := AnsiReplaceStr(Result, '/', '__'); |
299 |
Result := AnsiReplaceStr(Result, '>', '__'); |
300 |
Result := AnsiReplaceStr(Result, '<', '__'); |
301 |
end; |
302 |
|
303 |
|
304 |
end. |