-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcnvbase.m
482 lines (392 loc) · 13.7 KB
/
cnvbase.m
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
function out = cnvbase( varargin )
%CNVBASE Convert from one arbitrary base & encoding to another
% -------------------------------------------------------------------------
% Usage:
% out = cnvbase( in, inBase, outBase )
% Inputs:
% in - char / integer / double array defining input number
% inBase - definition of input base encoding, in same type as in
% outBase - definition of output base encoding
% Outputs:
% out - value encoded in outBase with matching type
% Description:
% This converts from one base to another. This can be used to convert
% from standard bases like binary and decimal or more exotic bases
% like base-3. Only integer numbers are handled.
%
% The output encoding can also be set. You could encode in binary
% using 1s & 0s or Xs and Os.
%
% The order of appearence in the base encoding is the ranking. A base
% of '0123456789' is very different from a base of '9876543210'
% Examples:
% '1001010' = cnvbase('74','0123456789','01')
% 'XOOXOXO' = cnvbase('74','0123456789','OX')
% -------------------------------------------------------------------------
% Title: Convert between arbitrary bases and encodings
% Author: Robert Kagy
% Matlab Release: Tested on R13SP1
% Verify input & exit if problems are found
[bExit, in, inBase, outBase] = sVerifyInput( varargin{:} );
if strcmp(bExit,'Quit')
out = [];
return;
elseif strcmp(bExit,'SelfTest')
out = [];
sSelfTest;
return;
elseif strcmp(bExit,'FcnH')
out = sFcnH;
return;
else
% Convert from custom encoding to array of integers
inArray = sConvertToUniform(in, inBase);
% Convert from inBase to outBase
outArray = sConvertBases(inArray, length(inBase), length(outBase));
% Convert from array of integers to custom encoding
out = sConvertFromUniform(outArray, outBase);
end
% -------------------------------------------------------------------------
function [bExit, in, inBase, outBase] = sVerifyInput( varargin )
% Verify the input from the command line is syntaxtically correct
% Initialize outputs
bExit = 'Quit';
in = [];
inBase = [];
outBase = [];
if nargin == 0
% Run the self test
bExit = 'SelfTest';
return;
elseif nargin == 1
if ischar(varargin{1})
if strcmp('FcnH',varargin{1})
bExit = 'FcnH';
return;
else
% Display problem and exit
disp(['cnvbase requires exactly 3 inputs, but you provided ',int2str(nargin)]);
return;
end
else
% Display problem and exit
disp(['cnvbase requires exactly 3 inputs, but you provided ',int2str(nargin)]);
return;
end
elseif nargin ~= 3
% Display problem and exit
disp(['cnvbase requires exactly 3 inputs, but you provided ',int2str(nargin)]);
return;
else
% Divide up varargin
in = varargin{1};
inBase = varargin{2};
outBase = varargin{3};
end
% Ensure in & inBase are of the same class
if ~strcmp(class(in),class(inBase))
% Display problem and exit
disp(sprintf('cnvbase requires in & inBase be of the same class\n\tClass of in: %s\n\tClass of inBase: %s', ...
class(in), class(inBase)));
return;
end
% Ensure that all 3 are of valid classes
VALID_CLASSES = {'double','logical','char','int8','uint8','int16','uint16','int32','uint32'};
if ~ismember(class(in),VALID_CLASSES)
% Display problem and exit
disp(['in is of class ',class(in),' which is not supported']);
return;
elseif ~ismember(class(inBase),VALID_CLASSES)
% Display problem and exit
disp(['inBase is of class ',class(inBase),' which is not supported']);
return;
elseif ~ismember(class(outBase),VALID_CLASSES)
% Display problem and exit
disp(['outBase is of class ',class(outBase),' which is not supported']);
return;
end
% Ensure all elements of in are members of set inBase
if ~min(ismember(in,inBase))
% Display problem and exit
strErr = sprintf('inBase is not a superset of in\nValues not members of inBase:\n');
switch class(in)
case 'char'
strErr = [strErr, setdiff(in,inBase)];
case {'double','logical'}
strErr = [strErr, num2str(setdiff(in,inBase))];
otherwise
strErr = [strErr, num2str(double(setdiff(in,inBase)))];
end
disp(strErr);
return;
end
% Ensure can use intermediate encoding
MAX_LENGTH = 2^32-1;
if length(inBase) > MAX_LENGTH
% Display problem and exit
disp(['Max base handled is ',int2str(MAX_LENGTH),...
' but inBase is of length ',int2str(length(inBase))]);
return;
elseif length(outBase) > MAX_LENGTH
% Display problem and exit
disp(['Max base handled is ',int2str(MAX_LENGTH),...
' but inBase is of length ',int2str(length(outBase))]);
return;
end
% Ensure all elements are unique
if length(inBase) ~= length(unique(inBase))
% Display problem and exit
disp(['Not all elements of inBase are unique, invalid base']);
return;
elseif length(outBase) ~= length(unique(outBase))
% Display problem and exit
disp(['Not all elements of inBase are unique, invalid base']);
return;
end
% Made it so don't exit
bExit = 'Go';
% -------------------------------------------------------------------------
function inArray = sConvertToUniform(in, inBase)
% Convert from custom base to integer array
% Initialize inArray
inArray = [];
% Loop through each array element
for idx=1:length(in)
inArray = [inArray, find(in(idx) == inBase)];
end
% -------------------------------------------------------------------------
function out = sConvertFromUniform(outArray, outBase)
% Convert from integer array to custom base
% Create empty array of same class as outBase
switch class(outBase)
case 'double'
out = [];
case 'logical'
out = logical([]);
case 'char'
out = char([]);
case 'int8'
out = int8([]);
case 'int16'
out = int16([]);
case 'int32'
out = int32([]);
case 'uint8'
out = uint8([]);
case 'uint16'
out = uint16([]);
case 'uint32'
out = uint32([]);
otherwise
error(['sConvertFromUniform doesn''t support the ',...
class(outBase),' class!']);
end
% Convert one element at a time
for idx=1:length(outArray)
out = [out, outBase(outArray(idx))];
end
% -------------------------------------------------------------------------
function outArray = sConvertBases(inArray, inBase, outBase)
% convert integer array from inBase to outBase
% Check for simple case
if inBase == outBase
outArray = inArray;
return;
end
% Convert from array to array
% Less change of precision loss by
% working with one digit at a time
% Remove offset from inArray
inArray = inArray - 1;
% Initialize accumulator
outArray = [];
% Loop through digits
for idx = length(inArray):-1:1
% Get current digit and power
cDigit = inArray(idx);
cPower = length(inArray) - idx;
% Raise input base to power as represented in destination base
arrPower = sPowArbBases(inBase, cPower, outBase);
% Convert current digit to destination base
arrDigit = sConvertVal2Base(cDigit, outBase);
% Multiply current digit by current power
% All as represented in destination base
arrValue = sMultArbBases(arrPower, arrDigit, outBase);
% Add current digits value to accumulator
% All as represented in destination base
outArray = sAddArbBases(outArray, arrValue, outBase);
end
% Add offset to outArray
outArray = outArray + 1;
% -------------------------------------------------------------------------
function res = sConvertVal2Base(value, base)
% Converts a value to an integer array
% Initialize output
res = [];
% Loop until complete
while value > 0.5
remainder = rem(value, base);
res = [remainder, res];
value = fix( (value-remainder) / base );
end
% -------------------------------------------------------------------------
function res = sAddArbBases(arr1, arr2, base)
% This function adds two numbers defined as arrays of a certain base
% Initialize output
res = [];
carry = 0;
% Loop through arrays matching parts
commonLen = min(length(arr1),length(arr2));
for idx=1:commonLen
% Calculate current array position
cPos1 = length(arr1) - idx + 1;
cPos2 = length(arr2) - idx + 1;
% Calculate the value in this position
val = carry + arr1(cPos1) + arr2(cPos2);
% Calculate value for this position & carry
vPos = rem(val,base);
carry = (val-vPos) / base;
% Store and move on
res = [vPos, res];
end
% Continue with longer array
if length(arr1) > length(arr2)
for idx=1:(length(arr1)-commonLen)
% Calculate current array position
cPos = length(arr1) - commonLen - idx + 1;
% Calculate the value in this position
val = carry + arr1(cPos);
% Calculate value for this position & carry
vPos = rem(val,base);
carry = (val-vPos) / base;
% Store and move on
res = [vPos, res];
end
elseif length(arr2) > length(arr1)
for idx=1:(length(arr2)-commonLen)
% Calculate current array position
cPos = length(arr2) - commonLen - idx + 1;
% Calculate the value in this position
val = carry + arr2(cPos);
% Calculate value for this position & carry
vPos = rem(val,base);
carry = (val-vPos) / base;
% Store and move on
res = [vPos, res];
end
end
% Add carry to end if still there
res = [sConvertVal2Base(carry, base), res];
% -------------------------------------------------------------------------
function res = sMultArbBases(arr1, arr2, base)
% This function multiplies two numbers defined as arrays of a certain base
% Initialize output
res = [0];
% Loop through digits in 2nd
for idx=1:length(arr2)
% Find current digit and offset
cOff = idx-1;
cDig = arr2(length(arr2)-cOff);
% Multiple that arr1 by that digit
cVal = sMultArrByDigit(arr1, cDig, cOff, base);
% Add result of multiply to accumulator
res = sAddArbBases(res, cVal, base);
end
% -------------------------------------------------------------------------
function res = sMultArrByDigit(arr, val, offset, base)
% This function multiplies an array and a single value
% Initialize output
res = zeros(1,offset);
carry = 0;
% Multiply each digit in array
for idx=1:length(arr)
% Find current digit
cDig = arr(length(arr)-idx+1);
% Calculate the value in this position
cVal = (val * cDig) + carry;
% Calculate value for this position & carry
vPos = rem(cVal,base);
carry = (cVal-vPos) / base;
% Store and move on
res = [vPos, res];
end
% Add carry to end if still there
res = [sConvertVal2Base(carry, base), res];
% -------------------------------------------------------------------------
function res = sPowArbBases(value, pow, base)
% This function raises a number to a power and outputs
% an integer array in the desired base
% Check for 0
if pow == 0
res = 1;
else
% Convert value to desired base
x = sConvertVal2Base(value, base);
% Initialize output
res = 1;
% Keep going until pow is zero
while pow > 0
% Handle odd power by multiplication
if fix(pow/2) ~= (pow/2)
res = sMultArbBases(res, x, base);
if pow == 1
return;
else
pow = pow - 1;
end
end
% Handle even power by squaring
x = sMultArbBases(x, x, base);
pow = pow / 2;
end
end
% -------------------------------------------------------------------------
function sSelfTest
% Perform a self test / demo
% Intro
tic;
disp('cnvbase can convert between number bases on arbitrary large numbers');
disp(' Usage: cnvbase(valArray, inBase, outBase)');
disp(' ');
disp('-------- SELF TEST --------------');
disp('Same base:');
disp([' cnvbase(''101'',''01'',''01'') = ', cnvbase('101','01','01')]);
disp([' cnvbase(''101'',''01'',''AB'') = ', cnvbase('101','01','AB')]);
disp('dec2hex:');
disp([' dec2hex(3645) = ',dec2hex(3645)]);
disp([' cnvbase(''3645'',''0123456789'',''0123456789ABCDEF'') = ',...
cnvbase('3645','0123456789','0123456789ABCDEF')]);
disp('hex2dec:');
disp([' hex2dec(''F304FCA'') = ', int2str(hex2dec('F304FCA'))]);
disp([' cnvbase(''F304FCA'',''0123456789ABCDEF'',''0123456789'') = ', ...
cnvbase('F304FCA','0123456789ABCDEF','0123456789')]);
disp('Large Number:');
disp([' cnvbase(''FEDCBA9876543210'',''0123456789ABCDEF'',''01'') = ', ...
cnvbase('FEDCBA9876543210','0123456789ABCDEF','01')]);
disp([' cnvbase([''2'',int2str(2^53)],''0123456789'',''0123456789ABCDEF'') = ', ...
cnvbase(['2',int2str(2^53)],'0123456789','0123456789ABCDEF')]);
disp('Strange Bases:');
disp([' cnvbase(''3021'',''0123'',''012'') = ', ...
cnvbase('3021','0123','012')]);
disp([' cnvbase(''FEDCBA9876543210'',''0123456789ABCDEF'',''0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&*_+='') = ',...
cnvbase('FEDCBA9876543210','0123456789ABCDEF','0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&*_+=')]);
delta = toc;
disp(['Time to run Self Test: ', num2str(delta)]);
% -------------------------------------------------------------------------
function res = sFcnH
% Return a structure with function handles to all internal functions
% Useful for debugging
res = struct(...
'cnvbase', @cnvbase, ...
'sAddArbBases', @sAddArbBases, ...
'sConvertBases', @sConvertBases, ...
'sConvertFromUniform', @sConvertFromUniform, ...
'sConvertToUniform', @sConvertToUniform, ...
'sConvertValToBase', @sConvertValToBase, ...
'sFcnH', @sFcnH, ...
'sMultArbBases', @sMultArbBases, ...
'sMultArrByDigit', @sMultArrByDigit, ...
'sPowArbBases', @sPowArbBases, ...
'sSelfTest', @sSelfTest, ...
'sVerifyInput', @sVerifyInput ...
);