forked from ShaunRoselt/Roselt-Developer-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoselt.NumberBaseConversion.pas
More file actions
303 lines (269 loc) · 7.46 KB
/
Copy pathRoselt.NumberBaseConversion.pas
File metadata and controls
303 lines (269 loc) · 7.46 KB
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
{
In the future, I want functions to convert from and to for all of these:
Decimal (base 10)
Binary (base 2)
Hexadecimal (base 16)
Octal (base 8)
Sexagesimal (base 60)
Vigesimal (base 20)
Duodecimal (base 12)
Base 36
Balanced ternary (base 3)
Senary (base 6)
Quaternary (base 4)
Ternary (base 3)
Quinary (base 5)
Duotrigesimal (base 32)
Tridecimal (base 13)
Septenary (base 7)
Nonary (base 9)
Pentadecimal (base 15)
Hexatridecimal (base 23)
Base 64
}
{
Can use these sites to fact check my results:
https://calculator.name/baseconvert/decimal/duodecimal/0822004431
https://trustconverter.com/en/base-number-conversion/decimal/decimal-to-duodecimal.html
}
unit Roselt.NumberBaseConversion;
interface
uses
System.SysUtils,
System.StrUtils;
function IntToBin(Value: Cardinal; Digits: Integer): String; // old... need to deprecate
function IntToOct(Value: Integer): String; // old... need to deprecate
function HexadecimalToDecimal(Value: String): Int64;
function DecimalToOctal(Value: Int64): String;
function OctalToDecimal(Value: String): Int64;
function DecimalToBinary(Value: Int64): String;
function BinaryToDecimal(Value: String): Int64;
function DecimalToDuodecimal(decimal: Int64): string;
function DuodecimalToDecimal(duodecimal: String): Int64;
function FormatNumberString(Value: String; Spaces: Cardinal = 4): String;
function RemoveNonDecimalDigits(const S: string): string; // Removes all characters except numbers and negative -
function RemoveNonDuodecimalDigits(const S: string): string; // Removes all characters except 0123456789ABab
function RemoveNonHexadecimalCharacters(const S: string): string; // Removes all characters except 0123456789ABCDEFabcdef
function RemoveNonBinaryDigits(const Value: string): string; // Removes all characters except 0 and 1
function ConvertNumberBase(const input: string; fromBase, toBase: Integer): string; // One function to rule them all :D
// Need to properly test ConvertNumberBase() function and see if it works for everything
implementation
function IntToBin(Value: Cardinal; Digits: Integer): String;
var
S: String;
begin
S := '';
While Digits > 0 do
begin
if Odd(Value) then
S := '1' + S
Else
S := '0' + S;
Value := Value shr 1;
Digits := Digits - 1;
end;
Result := S;
end;
function IntToOct(Value: Integer): String;
const
tt: array [0 .. 7] of char = ('0', '1', '2', '3', '4', '5', '6', '7');
var
tempval: Integer;
begin
Result := '';
tempval := Value;
if tempval = 0 then
Result := '0'
else
while (tempval <> 0) do
begin
Result := tt[(tempval and $7)] + Result;
tempval := (tempval shr 3);
end;
end;
function HexadecimalToDecimal(Value: string): Int64;
var
HexValue: Int64;
begin
Result := 0;
for var i := 1 to Length(Value) do
begin
HexValue := StrToInt('$' + Value[i]);
Result := Result * 16 + HexValue;
end;
end;
function DecimalToOctal(Value: Int64): string;
const
tt: array [0 .. 7] of char = ('0', '1', '2', '3', '4', '5', '6', '7');
var
tempval: Int64;
begin
Result := '';
tempval := Value;
if tempval = 0 then
Result := '0'
else
while (tempval <> 0) do
begin
Result := tt[(tempval and $7)] + Result;
tempval := (tempval shr 3);
end;
end;
function OctalToDecimal(Value: String): Int64;
var
OctValue: Int64;
begin
Result := 0;
for var i := 1 to Length(Value) do
begin
OctValue := StrToInt('$' + Value[i]);
Result := Result * 8 + OctValue;
end;
end;
function DecimalToBinary(Value: Int64): string;
const
tt: array [0 .. 1] of char = ('0', '1');
var
tempval: Int64;
begin
Result := '';
tempval := Value;
if tempval = 0 then
Result := '0'
else
while (tempval <> 0) do
begin
Result := tt[(tempval and $1)] + Result;
tempval := (tempval shr 1);
end;
end;
function BinaryToDecimal(Value: String): Int64;
var
BinValue: Int64;
begin
Result := 0;
for var i := 1 to Length(Value) do
begin
BinValue := StrToInt('$' + Value[i]);
Result := Result * 2 + BinValue;
end;
end;
function DecimalToDuodecimal(decimal: Int64): string;
var
remainder: Int64;
duodecimal: string;
begin
duodecimal := '';
while decimal > 0 do
begin
remainder := decimal mod 12;
if remainder >= 10 then
duodecimal := Chr(Ord('A') + remainder - 10) + duodecimal
else
duodecimal := remainder.ToString + duodecimal;
decimal := decimal div 12;
end;
Result := duodecimal;
end;
function DuodecimalToDecimal(duodecimal: String): Int64;
var
decimal: Int64;
begin
decimal := 0;
for var i := 1 to Length(duodecimal) do
begin
if (duodecimal[i] >= '0') and (duodecimal[i] <= '9') then
decimal := decimal * 12 + (Ord(duodecimal[i]) - Ord('0'))
else if (duodecimal[i] >= 'A') and (duodecimal[i] <= 'B') then
decimal := decimal * 12 + (Ord(duodecimal[i]) - Ord('A') + 10);
end;
Result := decimal;
end;
function FormatNumberString(Value: String; Spaces: Cardinal): String;
begin
Result := '';
Value := ReverseString(Value);
for var I := 1 to Value.Length do
begin
Result := Result + Value[I];
if (I mod Spaces) = 0 then
Result := Result + ' ';
end;
Result := Trim(ReverseString(Result));
end;
function RemoveNonDecimalDigits(const S: string): string;
begin
SetLength(Result, S.Length);
var LActualLength := 0;
for var i := 1 to S.Length do
if CharInSet(S[i], ['0'..'9']) OR (S[i] = '-') then
begin
Inc(LActualLength);
Result[LActualLength] := S[i];
end;
SetLength(Result, LActualLength);
end;
function RemoveNonDuodecimalDigits(const S: string): string;
begin
SetLength(Result, S.Length);
var LActualLength := 0;
for var i := 1 to S.Length do
if CharInSet(S[i], ['0'..'9']) OR CharInSet(S[i], ['A'..'B']) OR CharInSet(S[i], ['a'..'b']) then
begin
Inc(LActualLength);
Result[LActualLength] := S[i];
end;
SetLength(Result, LActualLength);
end;
function RemoveNonHexadecimalCharacters(const S: string): string;
begin
SetLength(Result, S.Length);
var LActualLength := 0;
for var i := 1 to S.Length do
if CharInSet(S[i], ['0'..'9']) OR CharInSet(S[i], ['A'..'F']) OR CharInSet(S[i], ['a'..'f']) then
begin
Inc(LActualLength);
Result[LActualLength] := S[i];
end;
SetLength(Result, LActualLength);
end;
function RemoveNonBinaryDigits(const Value: string): string;
begin
SetLength(Result, Value.Length);
var LActualLength := 0;
for var i := 1 to Value.Length do
if (Value[i] = '0') OR (Value[i] = '1') then
begin
Inc(LActualLength);
Result[LActualLength] := Value[i];
end;
SetLength(Result, LActualLength);
end;
function ConvertNumberBase(const input: string; fromBase, toBase: Integer): string;
var
num, remainder: Int64;
begin
num := 0;
for var i := 1 to input.Length do
begin
if (Ord(input[i]) >= Ord('0')) and (Ord(input[i]) <= Ord('9')) then
num := num * fromBase + (Ord(input[i]) - Ord('0'))
else if (Ord(input[i]) >= Ord('A')) and (Ord(input[i]) <= Ord('Z')) then
num := num * fromBase + (Ord(input[i]) - Ord('A') + 10)
else if (Ord(input[i]) >= Ord('a')) and (Ord(input[i]) <= Ord('z')) then
num := num * fromBase + (Ord(input[i]) - Ord('a') + 10);
end;
result := '';
while num > 0 do
begin
remainder := num mod toBase;
if remainder < 10 then
result := Chr(remainder + Ord('0')) + result
else
result := Chr(remainder - 10 + Ord('A')) + result;
num := num div toBase;
end;
if result = '' then
result := '0';
end;
end.